Tapping near a button on navigation invokes the button - unexpected iPhone behaviour
I have a UIViewController class with a navigation bar shown and a button on the rightmost end of the navigation bar. Then right underneath it is my main content area with a UISegmentedControl shown below the navigation bar.
Whenever I click/tap on the navigation bar near the right button but not directly on the button, I am getting an unexpected button press event. Usually, it's when I'm trying tap on the right most segment item that happens to be placed beneath the button. So the tap is on the button instead of the segment. How do I make sure the event is directed to the right receiver?
My UI is created with Interface Builder and the UISegmentedControl is hooked up to my class in the nib file.
My class:
@interface MyViewController : UIViewController
{
IBOutlet UISegmentedControl *segmentedControl;
}
@end
@implementation MyViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
target:self action:@selector(onButtonPressed:)]; 开发者_开发问答
self.navigationItem.rightBarButtonItem = button;
[button release];
}
- (IBAction)onButtonPressed:(id)sender
{
NSLog(@"onButtonPressed reached!");
}
- (IBAction)onSegmentItemPressed:(id)sender
{
NSLog(@"onSegmentItemPressed for: %d", [segmentedControl selectedSegmentIndex]);
}
@end
This is standard behavior. It is designed to make those small buttons easier to press. You can try it in any app with a navbar and you will get the same result. Without seeing the layout of your app, I would think you are either missing the segment (try tapping more carefully) or the segment is too close to the navbar button (move it slightly).
It may be possible to use the hitTest:
methods to determine whether or not the touch was in the segment, and manually activate it, but I would think this is more trouble than it's worth.
You may need to move the segmented control's origin further down, vertically, away from the navigation bar button item, to prevent unexpected taps.
精彩评论