简体   繁体   中英

iOS - Set UIToolBarItem on UINavigationController

I drag out a toolbaritem in storyboard and set it on my nav controller, but when I run my code it's not there, is there something I'm missing?

EDIT:

Tried setting it in code as well in my viewDidLoad method:

UIBarButtonItem *rightBarButton = [[UIBarButtonItem alloc] initWithTitle:@"Map" style:UIBarButtonItemStyleBordered target:self action:@selector(viewMap)];
self.navigationItem.rightBarButtonItem = rightBarButton;

Won't work either.

Here's how it's set up in my storyboard:

在此处输入图片说明

UPDATE:

Just found my problem. In my controller code when I update it's contents I change the right bar button item for a spinner and never set it back to what it had before.

UINavigationController already has a toolbar built in. It has a property toolBarHidden which is set to YES by default, which is why it is not normally seen. If you are using storyboard you can easily make the built-in bottom toolbar visible by checking the checkbox "Shows Toolbar" in the inspector when the Navigation Controller is selected.

See the UINavigationController documentation here for more details.

EDIT:

Ok, it sounds like what you are trying to do is add a right button to your view controller's UINavigationItem . To do this in storyboard, drag a "Bar Button Item" from the Objects Library onto the Navigation Item in your ViewController. You can then set the title/style/etc of the bar button item. If nothing still shows up when you run your app, make sure that your ViewController is connected properly with a segue to the navigation controller.

Also make sure you are adding the Bar Button Item to your view controller's Navigation Item, NOT to the View Controller itself. Here is how the setup should look in your storyboard:

在此处输入图片说明

To add an item to a navigation bar, you need to add a Bar Button Item to the Navigation Item contained in the view controller. Go to your storyboard, find the right VC, and find the navigation item (it's in the hierarchy shown in the navigation controller 'scene'). Just drag a Bar Button Item into that hierarchy underneath the nav item, or directly onto the navbar in the visual builder display.

The navigation controller only looks at your VC's nav item when that VC is pushed onto the stack; hence modifying the VC's nav item in viewDidLoad has no effect.

(I've done this programmatically before but I don't have the code with me, so maybe I'll add that later...)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM