backButton = [UIButton buttonWithType:UIButtonTypeCustom];
[backButton addTarget:self action:@selector(gotoAmphorasViewController) forControlEvents:UIControlEventTouchUpInside];
[backButton setFrame:CGRectMake(0.0f,0.0f, 44,44)];
我面临的问题是,虽然按钮尺寸为44*44
,但只要点击它周围的任何地方,就会触发按钮操作。
答案 0 :(得分:1)
它不是一个错误。这是默认行为。在iPhone中,对于导航栏按钮,触摸检测稍微扩展而不是其框架。只需看看其他任何应用程序。如果我们在靠近它的框架之外点击按钮就会被触发。
答案 1 :(得分:1)
这是预期的行为,如果您真的想限制触摸区域,可以将按钮包裹在UIView中:
UIView *buttonContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
[buttonContainer addSubview:button];
_barButton = [[UIBarButtonItem alloc] initWithCustomView:buttonContainer];
答案 2 :(得分: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];
}