2

I have a navigation controller which I've added a UIButton to the navigation bar, but there's something wrong with it. Even if I tap way outside of the button, it still calls it's action.

This is how I create and add the button:

UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[backBtn setImage:[UIImage imageNamed:@"btn_back.png"] forState:UIControlStateNormal];
[backBtn addTarget:self action:@selector(backPressed:) forControlEvents:UIControlEventTouchUpInside];   
[backBtn setFrame:CGRectMake(0, 0, 70, 30)];
    //[backBtn setFrame:CGRectMake(20, 7, 70, 30)];

self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:backBtn] autorelease];

Here's the tap range of the button:

http://www.flickr.com/photos/98950925@N07/9465901718

Marcus Adams
  • 53,009
  • 9
  • 91
  • 143
Lord Zsolt
  • 6,492
  • 9
  • 46
  • 76
  • see also [Why does UINavigationBar steal touch events?](http://stackoverflow.com/q/9079907/643383) – Caleb Aug 08 '13 at 12:44

2 Answers2

1

It is by default size of left button and right button . So we can do nothing with left and right button. Because its area of left and right button. If you wanna custom button in that case it will show only your given frame but it'll work in left button's whole area.

Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
  • I means its by default size of left button ..and if you wanna custom button in that case it will show only your given frame but it'll work in left button's whole area. – Dharmbir Singh Aug 08 '13 at 12:36
  • Well thats for the answer, but I really think it's stupid... – Lord Zsolt Aug 08 '13 at 12:40
  • May be its stupid but you know what can we do if its by default functionality. – Dharmbir Singh Aug 08 '13 at 12:42
  • Add a fake plain button with empty title to leftBarButtonItems with an action that do nothing? – nerowolfe Aug 08 '13 at 12:44
  • @LordZsolt Have you ever heard a *user* complain that the back button was too easy to tap? I haven't. I can understand being surprised at this behavior once you notice it, but you've probably been using iOS for quite a while without noticing, right? Just avoid putting touchable UI elements right up against the nav bar (which is just common sense anyway) and your app will both work fine and be easier to use than if the back button's touch area were limited to its visible area. – Caleb Aug 08 '13 at 12:49
0

Try this.

UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"btn_back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(Back:)];
[self.navigationController setHidesBackButton:YES];
[self.navigationItem setLeftBarButtonItem: customItem]; 
Adarsh V C
  • 2,314
  • 1
  • 20
  • 37