0

的我實現這個自定義導航欄類​​爲我的項目:Video with IssueIOS定製UINavigationBar的 - barButtonItems接收觸摸出界

#import "PTTNavigationBar.h" 
@implementation PTTNavigationBar 

- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code 

} 
    return self; 
} 


// Overriding drawRect: perform custom drawing. 
// An empty implementation adversely affects performance during animation. 

- (void)drawRect:(CGRect)rect 
{ 
    // Drawing code 
    UIImage *navBarImage = UIIMAGE_NAMED(kNavBarBackGroundImage); 
    [navBarImage drawInRect:CGRectMake(0, 0, NAVBAR_SIZE.width , NAVBAR_SIZE.height)]; 

    [self setBackgroundColor:[UIColor clearColor]]; 

} 

- (CGSize)sizeThatFits:(CGSize)size { 

    //[self setTitleVerticalPositionAdjustment:-12 forBarMetrics:UIBarMetricsDefault]; 
    CGRect frame = [UIScreen mainScreen].applicationFrame; 
    CGSize newSize = CGSizeMake(frame.size.width , NAVBAR_SIZE.height); 
    [self layoutSubviews]; 

    return newSize; 
} 


-(void) layoutSubviews 
{ 
    [super layoutSubviews]; 

    [self setBackgroundColor:[UIColor clearColor]]; 

    for (UIView *view in self.subviews) 
    { 
     CGRect frame = view.frame; 
     frame.origin.y = 6; 
     view.frame = frame; 
    } 


} 

@end 

任何人都可以幫我這個? thx

+0

的邊界內的觸摸你是如何加入barButtonItems?他們是自定義按鈕嗎? –

+0

其後退按鈕。默認的一個 – luca

+0

這是iOS的默認功能,您可以在設置應用程序中檢查。如果你真的想限制觸摸,那麼我的答案將起作用。 –

回答

1

我假設你正在通過代碼向leftBarButton添加自定義按鈕。

下面的代碼應該限制barButtonItem

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
[button setImage:buttonImage forState:UIControlStateNormal]; 
button.frame = CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height); 
[button addTarget:self action: @selector(handleBackButton)forControlEvents:UIControlEventTouchUpInside]; 

UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, buttonImage.size.width, buttonImage.size.height)]; 
[view addSubview:button]; 

UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:view]; 
self.navigationItem.leftBarButtonItem = customBarItem; 
相關問題