I am trying to set items for a navigationController using the following call
NSArray *items = [NSArray arrayWithObjects: shareButton, nil];
[self.navigationController.toolbar setItems:items animated:NO];
This adds nothing to the toolbar.
I can hide and show the toolbar using
[self.navigationController setToolbarHidden:NO];
But cant make the items appear.
How does one set the items. ?
Update:
NavController manages app navigation within a NavHost . Apps will generally obtain a controller directly from a host, or by using one of the utility methods on the Navigation class rather than create a controller directly. Navigation flows and destinations are determined by the navigation graph owned by the controller.
toolbar is a read-only property. You set toolbars in this way:
toolbar The custom toolbar associated with the navigation controller. (read-only)
@property(nonatomic,readonly) UIToolbar *toolbar Discussion This property contains a reference to the built-in toolbar managed by the navigation controller. Access to this toolbar is provided solely for clients that want to present an action sheet from the toolbar. You should not modify the UIToolbar object directly.
Management of this toolbar’s contents is done through the custom view controllers associated with this navigation controller. For each view controller on the navigation stack, you can assign a custom set of toolbar items using the setToolbarItems:animated: method of UIViewController.
Edit: so you should do this:
[self setToolbarItems:items animated:NO];
Edit: here's how to add a right bar button item:
- (void) addRightButton
{
UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[rightBtn setImage:[UIImage imageNamed:@"mybutton.png"] forState:UIControlStateNormal];
rightBtn.frame = CGRectMake(0, 0, 70, 40 );
[rightBtn addTarget:self action:@selector(myButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *rightBarBtn = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
self.navigationItem.rightBarButtonItem = rightBarBtn;
}
Edit: to create flexible/fixed space items programmatically, use this:
- (id)initWithBarButtonSystemItem:(UIBarButtonSystemItem)systemItem target:(id)target action:(SEL)action
values settable for systemItem include UIBarButtonSystemItemFlexibleSpace and UIBarButtonSystemItemFixedSpace. Check the documentation for the UIBarButtonItem class:
http://developer.apple.com/library/ios/#documentation/uikit/reference/UIBarButtonItem_Class/Reference/Reference.html
Edit: the question was clarified. The toolbar along the bottom has nothing to do with the navigationItem or the navigation controller, it's just a UIToolbar. You need to either set it up entirely in IB or set up an outlet in your class and set it up / finalize it in code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With