BarButton项目正在点击导航栏时获取事件

时间:2013-10-23 12:45:51

标签: iphone ios6 ios7

我将自定义后退按钮添加到导航栏(自定义导航控制器),如下所示: -

UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:@"goback.png"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(handleBackButtonClick) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 32, 32)];//same as image width and height
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

当我点击靠近条形按钮项目(左右两侧)时,条形按钮正在触摸事件。

我想防止这种奇怪的行为。

我正在为iOS7开发应用。

1 个答案:

答案 0 :(得分:0)

我尝试调整barButtonItem的width属性而没有运气。

    self.navigationItem.leftBarButtonItem.width = 32;

但我有一个有趣的解决方案,

您可以在自定义视图中使用两个按钮,一个用于实现所需方法,另一个用于虚拟按钮

UIButton *button =  [UIButton buttonWithType:UIButtonTypeCustom];

[button setImage:[UIImage imageNamed:@"goBack.png"] forState:UIControlStateNormal];

[button addTarget:self action:@selector(handleBackButtonClick) forControlEvents:UIControlEventTouchUpInside];

[button setFrame:CGRectMake(0, 0, 32, 32)];//same as image width and height

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

UIButton *dummyButton = [UIButton buttonWithType:UIButtonTypeCustom];

UIBarButtonItem *dummyBarButton = [[UIBarButtonItem alloc] initWithCustomView:dummyButton];

self.navigationItem.leftBarButtonItems = @[barButton,dummyBarButton];