Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting toolbar items for a navigationController

Tags:

iphone

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:

enter image description here

like image 283
some_id Avatar asked Feb 21 '11 14:02

some_id


People also ask

What is a navigation controller?

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.


1 Answers

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.

like image 141
Bogatyr Avatar answered Nov 09 '22 07:11

Bogatyr