tags:

views:

71

answers:

2

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
A: 

You may need to move the segmented control's origin further down, vertically, away from the navigation bar button item, to prevent unexpected taps.

Alex Reynolds
how do I make it look like so there are no gaps in the visual of where the nav bar and segment control are appearing? I want them to look like one is sitting right below the other...
Alexi Groove
I don't think you can make it look like there are no gaps. If there are no gaps, then it is possible for a touch event to be slightly inaccurate and trigger the wrong button. Maybe you can put the segmented control directly in the navigation bar, instead.
Alex Reynolds
A: 

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.

David Kanarek