简体   繁体   中英

BarButton Items are getting events while clicking on navigation bar

I am adding custom back button to navigation bar(custom navigation controller) like this:-

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];

When I click close to bar button items(both left and right) the bar buttons are getting touch events.

I would like to prevent this weird behavior.

I am developing app for iOS7 .

I tried adjusting the width property of the barButtonItem and no luck.

    self.navigationItem.leftBarButtonItem.width = 32;

but I have an interesting solution for you,

You can use two buttons in your custom view, one that implements that method you want, and other a dummy button

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM