0

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

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.

4

1 回答 1

0

我尝试调整 barButtonItem 的宽度属性,但没有运气。

    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];
于 2013-10-23T13:36:34.610 回答