I am trying to set items for a navigationController using the following call
NSArray *items = [NSArray arrayWithObjects: shareBu开发者_如何学JAVAtton, 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:
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.
精彩评论