Alex Rivera | Logout

iOS - UINavigationController adding multiple right items?

Asked 2011-07-30T19:23:22.037
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?

Edit
Report

2 Answers

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?

answered 2011-07-30T20:03:29.257
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.

answered 2011-12-11T17:52:46.510

Your Answer