Ahsan
Ahsan

Reputation: 2982

Issue with button on the left side of the uinavigation bar

I have a button on the left side of the navigation bar. I also have a UIButton near that button.

Now when I click the button thats below the navigation bar, in many cases, the button on the nab bar gets clicked.

Any idea why this is happening? Any suggestions ?

Thanks.

image: if you click anywhere inside the red button, the top left button of the nav bar still gets pressed.

enter image description here

code for nav bar button:

UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 25, 25)];
[leftButton setShowsTouchWhenHighlighted:NO];
[leftButton setImage:[UIImage imageNamed:@"deals.png"] forState:UIControlStateNormal];
[leftButton setImage:[UIImage imageNamed:@"deals_down.png"] forState:UIControlStateSelected];
[leftButton setImage:[UIImage imageNamed:@"deals_down.png"] forState:UIControlStateHighlighted];
[leftButton addTarget:self action:@selector(dealsButtonPressed:) forControlEvents:UIControlEventTouchDown || UIControlEventTouchUpInside];
self.navigationItem.leftBarButtonItem =  [[[UIBarButtonItem alloc] initWithCustomView:leftButton] autorelease];
[leftButton release];

Upvotes: 3

Views: 2279

Answers (3)

Martin
Martin

Reputation: 12215

Note that if Apple enlarged the navigationBar's left button (right one is not), it's for usability questions. It's almost the most clicked button of iphone device, so people have got the ability to tap back button in an instinctive way (i never look at the button, I just know the movement).

Have in mind that if you reduce the touch region, users may have difficulties to reach it.

My recommandation, sorry for that, would be to move down 3 or 5px down your 3 buttons.

Upvotes: 2

Ash Furrow
Ash Furrow

Reputation: 12421

It looks like a problem with how you're initializing the UIButton instance. Typically, we would use [UIButton buttonWithType:custom]; in your situation.

From this answer, they solved a similar question with the following:

UIImage *myImage = [UIImage imageNamed:@"paw.png"];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setImage:myImage forState:UIControlStateNormal];
myButton.showsTouchWhenHighlighted = YES;
myButton.frame = CGRectMake(0.0, 0.0, myImage.size.width, myImage.size.height);

[myButton addTarget:self action:@selector(tapped:) forControlEvents:UIControlEventTouchUpInside];

UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithCustomView:myButton];
self.navigationItem.rightBarButtonItem = rightButton;

[rightButton release];
[myButton release];

It's not the code you're looking for, but it shows you how to correctly instantiate a UIButton instance. Hopefully that will sort out the problems you're having.

Upvotes: 1

fbernardo
fbernardo

Reputation: 10104

UINavigationButtons have a much larger tappable area than it's view, Apple made it this way for ease of use. If you really want to have two buttons that close to each other you have to subclass UINavigationBar, more specifically, override the touch events, get the touch coordinates and act as you see fit. More info here.

Upvotes: 12

Related Questions