Raj
Raj

Reputation: 1139

Adding two right bar button items to the navigation bar

I have a navigation bar to which I have added right BarButton successfully. Both the NavigationBar and BarButton are created programmatically. Now according to my requirement I got to add two right BarButtons to my navigation Bar. Can anyone tell me how to do this? My app is targeting ios4.

Upvotes: 8

Views: 12733

Answers (7)

Stefan Ovomate
Stefan Ovomate

Reputation: 511

Here is a swift example:

    let add = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addItem))
    let share = UIBarButtonItem(barButtonSystemItem: .action, target: self, action: #selector(shareTapped))

    navigationItem.rightBarButtonItems = [share, add]

Upvotes: 0

Shantanu
Shantanu

Reputation: 3136

This code will do the trick for you,

 NSArray *barButtonItems= [[NSArray alloc] initWithObjects:self.addButton,self.sortbyButton,nil];
self.navigationItem.rightBarButtonItems=barButtonItems;

where addButton and sortbyButton are 2 separate BarButton Items

Upvotes: 16

Siby
Siby

Reputation: 318

I know it is too late but I faced it recently. So here is what I did Create a UIView in code and add the buttons as subviews into this view. Create a ToolbarButton using [[UIBarButtonItem alloc] initWithCustomView:buttons]

Assign this toolbar button as the Left or right barbuttonItem as U wish.

Upvotes: 2

BornCoder
BornCoder

Reputation: 1066

NSArray *segmentTextContent = [NSArray arrayWithObjects:
                               NSLocalizedString(@\"Group By\", @\"\"),
                               NSLocalizedString(@\"Filter By\", @\"\"),
                               nil];
UISegmentedcontrol *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentTextContent];
segmentedControl.selectedSegmentIndex = 0;
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.frame = CGRectMake(0, 0, 125, 30);
[segmentedControl addTarget:self action:@selector(toggleUnit) forControlEvents:UIControlEventValueChanged];

segmentedControl.tintColor = [UIColor lightGrayColor];
defaultTintColor = [segmentedControl.tintColor retain];

self.navigationItem.rightBarButtonItem = segmentedControl;
[segmentedControl release];

Upvotes: 0

BornCoder
BornCoder

Reputation: 1066

@Matthias, As stated in documentation, rightBarButtonItems property is available from iOS 5 and above and this function needs to be supported also on iOS 4.

So, UISegmentControl is best way to achieve this.

Upvotes: 0

Matthias
Matthias

Reputation: 8180

As the documentation to UINavigationItem1 describes, it has a property rightBarButtonItems (as well as leftBarButtonItems) where you can give an array of UIBarButtons. They are display from right (index 0) to left (index n-1).

Upvotes: 0

BornCoder
BornCoder

Reputation: 1066

If you application is targeting iOS 4 and above then you should take UISegmentControl and have two segments on it. Catch value changed action event and check which segment is selected and do you operation accordingly.

You can also set images to segments to make look and feel better.

Upvotes: 0

Related Questions