Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add two UIBarButtonItems to UINavigationItem?

I want two rightBarButtonItem's on my UINavigationBar. How can I accomplish this?

like image 510
Sheehan Alam Avatar asked Jun 09 '10 17:06

Sheehan Alam


1 Answers

You can use a UISegmentedControl with two buttons and configure it with the momentary property set to YES.

This is what is used in the Mail application to go to next/previous message.

Update

In order to assign the UISegmentedControl]1 as a right button, you have to wrap it inside a UIBarButtonItem (sample code taken from the NavBar sample application):

- (void)viewDidLoad
{
    // "Segmented" control to the right
    UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
                                                [NSArray arrayWithObjects:
                                                    [UIImage imageNamed:@"up.png"],
                                                    [UIImage imageNamed:@"down.png"],
                                                 nil]];
    [segmentedControl addTarget:self action:@selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
    segmentedControl.frame = CGRectMake(0, 0, 90, kCustomButtonHeight);
    segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
    segmentedControl.momentary = YES;

    UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
    [segmentedControl release];

    self.navigationItem.rightBarButtonItem = segmentBarItem;
    [segmentBarItem release];
}
like image 63
Laurent Etiemble Avatar answered Nov 10 '22 08:11

Laurent Etiemble