2014-02-06 223 views
0
backButton = [UIButton buttonWithType:UIButtonTypeCustom]; 

[backButton addTarget:self action:@selector(gotoAmphorasViewController) forControlEvents:UIControlEventTouchUpInside]; 

[backButton setFrame:CGRectMake(0.0f,0.0f, 44,44)]; 

我面临的问题是,尽管按钮尺寸为44*44,无论我在哪里点击它的任何位置,按钮操作都会被触发。导航栏自定义按钮

+0

为什么那么糟糕?您总是可以通过按钮获得额外的回旋余地,以便轻松按下。 – Mika

+0

我不是说这很糟糕,我对此没有任何想法,所以我认为这是一个错误。 谢谢@ mMikael – Abhishek

回答

1

请尝试娄代码:它的正常工作

- (void)viewDidLoad { 
    [super viewDidLoad]; 
UIImage *buttonImage = [UIImage imageNamed:@"back.png"]; 

    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(back) forControlEvents:UIControlEventTouchUpInside]; 

    UIBarButtonItem *customBarItem = [[UIBarButtonItem alloc] initWithCustomView:button]; 

    self.navigationItem.leftBarButtonItem = customBarItem; 
    [customBarItem release]; 



} 

-(void)back { 

[self.navigationController popViewControllerAnimated:YES]; 
} 
1

它不是一个错误。这是一种默认行为。在iPhone中,对于导航栏按钮,触摸检测稍微多了一些,而不是它的框架。只需看看任何其他应用程序。如果我们点击更近但在框外,按钮就会被触发。

1

这是预期的行为,如果你真的想限制触摸的区域,你可以用一个UIView里面的按钮:

UIView *buttonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)]; 
[buttonContainer addSubview:button]; 
_barButton = [[UIBarButtonItem alloc] initWithCustomView:buttonContainer]; 
+0

谢谢男人:) 我喜欢你的方法 – Abhishek