43

I have set up a view controller in Storyboard which is embedded in a navigation controller. In this nav controller I have checked Shows Navigation Bar and Shows Toolbar and enabled a navigation bar and toolbar in Top Bar and Bottom Bar respectively in Simulated Metrics. I then "Show e.g. Push" segue from the view controller to another view controller. On both view controllers I've set up Simulated Metrics the same way, so both the navigation bar and toolbar are visible on all three controllers.

The problem is, I can add bar button items to the first view controller to both the navigation bar and the toolbar, but I cannot add a bar button item to the navigation bar on the second view controller. When I drop a button on the navigation bar, it's added to the toolbar instead. And I cannot double click the nav bar to add a title. In the outline I see there is no navigation item on the second view controller, but it is there in the first view controller.

I can probably add buttons to the navigation bar programmatically, but I want to do this visually in Storyboard. My question is, what is wrong with this setup, or is this a bug with Xcode?

enter image description here

enter image description here

Jordan H
  • 52,571
  • 37
  • 201
  • 351
  • See also another related problem that occurs in Xcode 8: http://stackoverflow.com/questions/24717573/storyboard-loses-navigation-bar-when-segueing-via-tap-gesture#24718591 – Efren Apr 11 '17 at 01:11

7 Answers7

91

For XCode 6, the UINavigationItem for the 2nd view controller onwards is not added automatically on the View Controller Object inside the storyboard. You will have to drag the UINavigationItem onto the Navigation Bar for that view Controller Object before adding UIBarButtonItem on top of it.

I am not sure why it is designed that way. I only discovered about this a few weeks ago.

Xcode 6 UiNavigationItem

Ricky
  • 10,485
  • 6
  • 36
  • 49
  • Actually, I don't know it is a bug. I thought it is designed that way for the developer to have more control. – Ricky Aug 31 '14 at 02:06
  • I'll report it, they'll know if that's expected behavior or not. :) – Jordan H Aug 31 '14 at 02:07
  • 1
    This is definitely NOT the reason. I'm using Xcode 6.4, it **does** add navigation items automatically (If there is a title, there is an `UINavigationItem` as well. And I can see it, click it, change the title on it so on so on so on. Everything works except adding buttons. Re-adding (overriding) the item DOES help indeed. So thanks! I bet my unborn kids, it's a bug! – Julian F. Weinert Jul 10 '15 at 13:07
  • @Julian Maybe they fixed the behaviour in 6.4 because I'm using 6.2 and the `Navigation Item` was definitely not there until I added it. – MCB Aug 10 '15 at 00:04
  • Great solution! – Mr T Apr 18 '19 at 10:17
12

If you have a ton of view controllers and very little custom auto-layout stuff in place, you can disable size classes, then re-enable it will add all missing nav bars across your storyboard.

enter image description here

enter image description here

Obviously not recommended if you have a ton of custom auto layout stuff linked up.

If you have just a single UIViewController that's missing a nav bar, just drop a UINavigationItem onto it.

William T.
  • 12,831
  • 4
  • 56
  • 53
3

If you're using Xcode 7 beta 4/5, try restarting Xcode it solved the issue for me

jrenouard
  • 407
  • 6
  • 8
1

I just bumped into this issue and it seems to be affected by the segue that shows the view controller.

If "Kind" setting in segue is "Show (e.g. Push)", it's not possible to drag the item to top right corner. However, if you explicitly change "Kind" setting to "Push", this can be done.

Edit: actually I just noticed that "Push" is deprecated. Not sure what Apple is thinking here. However, you can just change it back to "Show" after adding the button and it seems to work :D

ooxio
  • 806
  • 1
  • 10
  • 13
  • Yes, this is weird that you have to change it to (deprecated) Push and then back to Show for the Navigation Item to appear in the list – Bradley Thomas Mar 21 '19 at 14:37
0

For second view controller in hierarchy, you can setup title in attributes, without adding "navigation item" enter image description here

Daniil Bystrov
  • 303
  • 2
  • 12
0

I think it is a bug. I had the same problem.I fixed this problem by disable the size classes, then enable it.

You can disable and enable the size classes in Interface builder doc.

enter image description here

shim
  • 9,289
  • 12
  • 69
  • 108
Hongyang
  • 79
  • 1
  • 4
  • This is same answer as [William T's answer](http://stackoverflow.com/a/29159150/199364) from a year ago. Please read existing answers before adding a new answer to old questions. Thanks. – ToolmakerSteve Mar 18 '17 at 03:21
0

It's very simple. You just need to use navigaitonItem first as the holder of the buttons and then you can add barButtonItems on top of that.

emido333
  • 144
  • 4
  • Can you expand on this? – Caleb Kleveter Apr 21 '17 at 21:59
  • Yes, sure. follow the steps: 1- drag a navItem from the object library to your viewController, 2- drag a barButtonItem from the library and place it on top of the navigationItem that you've just added to the view. 3- run the application and you should have the barButtonItem on the screen now. – emido333 Apr 21 '17 at 22:06