33

I have a UINavigationController. I'm trying to add multiple buttons on the right side of my navigationBar. How can I achieve this? What kind of button does it take? UIBarButton or UINavigationItem?

aryaxt
  • 76,198
  • 92
  • 293
  • 442

6 Answers6

80

As of iOS5 you can assign an array of bar button items to the navigation item's rightBarButtonItems (note the plural) property.

jrturton
  • 118,105
  • 32
  • 252
  • 268
27

I used JRTurtons answer in Xcode 4.5, iOS 6 and implemented it like this and it works:

// Two buttons at the right side of nav bar
UIBarButtonItem *addAttachButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAttachmentClicked:)];
UIBarButtonItem *sendButton = [[UIBarButtonItem alloc] initWithTitle:LS(@"Send") style:UIBarButtonItemStyleBordered target:self action:@selector(sendClicked:)];
self.navigationItem.rightBarButtonItems = @[addAttachButton,sendButton];

However, I should mention, that UIBarButtonSystemItemPageCurl doesn't work like that.

Denis Kutlubaev
  • 15,320
  • 6
  • 84
  • 70
14

I am sure I read in the developer reference that additional buttons in the navigation bar is frowned upon. I cannot find that passage now. I have not done it myself, but found this link that seems to outline exactly what you need to do: (http://www.mattdipasquale.com/blog/2010/11/02/how-to-add-multiple-uibarbuttonitems-to-uinavigationbar/)

Have you considered using the toolbar property of the navigation controller?

Dean Davids
  • 4,174
  • 2
  • 30
  • 44
  • Thanks, that's exactly what I was looking for. It's a little bit hacky but it seems to be the only way to add multiple items to a UINavigationBar – aryaxt Jul 30 '11 at 20:48
  • 3
    If you're correct that additional right bar buttons were officially frowned upon, the new property in iOS5 noted by jrturton is evidence that Apple is willing to change its mind. Very encouraging. (And it means that the above hack will probably be accepted upon submission.) – Wienke Jul 05 '12 at 00:56
  • the answer below should be the correct answer to the question imho – dev_mush Apr 07 '16 at 10:18
  • Of course, iOS 5 was not yet released when this answer posted. But thanks anyway. – Dean Davids Apr 07 '16 at 12:22
  • 2
    The link no longer works and the explanation is no longer applicable. Can you please update? – Rob Norback Aug 15 '16 at 19:00
  • 404 file not found. – R. Mohan Jan 08 '19 at 08:30
1

adding any design to the navigation controller in XCode is easy.

add a UIView to your scene add the buttons you need to the UIView then drag and drop the UIView to the right space in the navigationController

mihai
  • 4,184
  • 3
  • 26
  • 27
1

In Xcode 7.1 (perhaps even earlier), you can add multiple items to the right or left side of a UINavigationBar just by dragging them in. If you drag to just the right spot, you get a little vertical bar that indicates where that item will be inserted.

Rick
  • 3,298
  • 3
  • 29
  • 47
0

there's actually even a bit ore hacky, but at the same time more cleaner way of doing this stuff: just define a category on UINavigationItem, like:

UINavigationItem+Toolbars.h:

@interface UINavigationItem (Toolbars)

@property (nonatomic, retain) IBOutlet UIToolbar * rightToolBar;
@property (nonatomic, retain) IBOutlet UIToolbar * leftToolBar;

- (void)setRightToolBar:(UIToolbar *)_rightToolBar;
- (UIToolbar *)rightToolBar;
- (void)setLeftToolBar:(UIToolbar *)_leftToolBar;
- (UIToolbar *)leftToolBar;

@end

UINavigationItem+Toolbars.m:

#import "UINavigationItem+Toolbars.h"

@implementation UINavigationItem (Toolbars)

- (void)setRightToolBar:(UIToolbar *)_rightToolBar {
    self.rightBarButtonItems = _rightToolBar.items;
}

- (UIToolbar *)rightToolBar {
    return nil;
}

- (void)setLeftToolBar:(UIToolbar *)_leftToolBar {
    self.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:_leftToolBar] autorelease];
}

- (UIToolbar *)leftToolBar {
    return nil;
}

@end

After doing that, just assign an outlet in IB settings a toolbar (just create one) and enjoy buttons appearing on the navigation item.

Anton
  • 2,483
  • 2
  • 23
  • 35
  • 2
    As of ios5 you can just assign an array of bar button items as the `rightBarButtonItems` property. – jrturton Dec 11 '11 at 18:28
  • Yep, you can, but how do you lay them out in IB? I personally prefer to have everything placed on a toolbar to see the visial appearance of the buttons next to each other. – Anton Dec 11 '11 at 20:18