iOS navigation has no navigation item - ios

In the current version of Xcode a few things seems to work a bit different.
I have a little problem working with a simple navigation. This is what I do:
I have a Storyboard with two scenes.
I connect the scenes with a segue of type "Show".
(In the past I did use "Push" but that is deprecated now.)
I select the first scene and choose "Embed in Navigation Controller.
Now both scenes will get a gray bar at the top but only the first scene will also get a Navigation Item.
Only for the first scene I am able to edit a title attribute and only for the first scene I am able to add a Bar Button.
Navigation itself works fine. The title of the first scene is the text of the Back-button in the second scene.
Why has the second scene no Navigation Item?
Is it because of the segue type?

Yeah, its a bug in Xcode from version 6. If you want Navigation Item in second view controller too, then there is a hack to get it.
Change the seque type from show to push. Now Navigation Item will appear in second VC too. You can again change the seque type from push to show, the Navigation Item will still present in second VC.

IB will automatically add UINavigationItem to root view controller of your UINavigationController.
To the rest of your controllers in navigation flow you need to add UINavigationItem manually from IB Object library.

What I do is to drag a navigation item from the list of controls to the top, gray bar on the second ViewController. Then you can drag other items such as bar buttons onto it, as well as being able to edit the title.

Related

Where is UINavigationItem disappearing to when refactoring views to storyboard?

I'm rewriting an Obj-C project using Swift 4 / Xcode 9.2
I have a large storyboard that I am breaking down into multiple storyboards. I did this previously without any issues. If I refactored a few views to their own storyboard then the views took the navigation item with them. now they disappear...
[
My questions are ...
Is this an Xcode bug? or a new feature?
Obviously you can work around this as I have shown in the pictures but is there a way to stop the vanishing from happening?
This is the way it's always been. In your storyboard, if a view controller is not a child of a navigation controller via root view controller or push segues, then by default no navigation bar is shown in the storyboard. You can force it on, though, by going to the Simulated Metrics section of the Attributes Inspector tab and selecting one of the navigation bar options for "Top Bar".
Your view controller is no longer a child of a navigation controller, so it no longer automatically has a navigation item. If you need it to have a navigation item, drag a Navigation Item from the Object Library onto it. You will then be able to give the navigation item a title, and drag buttons and so forth onto the navigation item.

When using Show instead of Push, which is deprecated, with UINavigationController, the VC being shown doesn't have navigationItem

Alright, here is the deal.
I have a UINavigationController which has a root VC. From this root VC, you can show another VC, and from there, another, and from there, another.
The problem is that I'm running into what I think is a bug in Xcode.
In the below image, you see a button in my rootVC navigationItem that I use to segue with Show to display the second VC. You can clearly see the Navigation Bar, but when I try to drag a UIBarButtonItem into it, I can't and the reason is because if you look at the menu on the left, you'll see that there isn't a Navigation Item to drag the button into!
If I instead use the segue Push to display the second VC, I get a navigationItem... but apparently Push is deprecated.
How do I mitigate this?
Edit: I guess I can just drag in a UINavigationItem into it... but I really don't know if I should. Would there be an empty UINavigationItem underneath that that I just don't see in storyboard but is actually there?
The navigation item is not created automatically on the second view controller, you're right. Drag a new navigation item into the second viewcontroller's bar, and you should be good to go.

How to add buttons to navigation controller visible after segueing?

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?
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.
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.
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.
If you're using Xcode 7 beta 4/5, try restarting Xcode it solved the issue for me
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
For second view controller in hierarchy, you can setup title in attributes, without adding "navigation item"
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.
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.

Custom segue causes navigation item to disappear

I'm using custom segues with no animation because I don't like the animation of the default segue. The problem is that in the storyboard, when I flip the segue style from Push to Custom, the view controller's navigation item disappears. Now, it seems that the navigation item still works even if the segue is set to Custom, but that means constantly flipping back and forth between Push and Custom when changes need to be made.
Is there any way to use a custom segue and still have IB access to the navigation item?
Yes, select the view controller and open the attributes inspector. Then change the "Top Bar" from "Inferred" to "Navigation Bar". That should keep the navigation bar displayed.

Using Xcode Storyboard to create Master-Detail views where the Detail view is Tab Bar to three different views

I'm trying to implementing master-detail scenes where the detail scene can be tabbed to show three different scenes. The example is a list of stories and for each story, I am interested to present an introduction, author info and reviews. So the scenes are as the following:
Master scene: a list of stories
When a story is selected:
Detail - Introduction scene (about the selected story)
Detail - Author scene (about the author)
Detail - Reviews scene (a list of reviews)
I attached the storyboard that I used to kind of achieve this:
https://docs.google.com/file/d/0B6I7Mlt7gqCBVTFZR2lsX0Q4MUk/edit?usp=sharing
I also attached the resultant scene on the iPhone simulator that showed the Detail - Author scene:
https://docs.google.com/file/d/0B6I7Mlt7gqCBQWhLTlNwWjZ2TG8/edit?pli=1
My problem is with there are 2 navigation bars presented in the detail - author scene: The top one with Stories back button, and the bottom one with Author label and B button. I want them to be collapsed into just one navigation bar. This means that when I click on a story from the Stories scene to transition to the detail author view, there will be only one navigation bar that has the Stories back button, the Author label, and the B button.
My question is: How can I collapse these two navigation bars together? Any advice would be greatly appreciated.
The problem is that you have a root navigation controller and you also have a navigation controller in each tab of the tab controller, then you show them both at the same time.
You could try a couple of things:
Hide the root nav controllers nav bar when the tab view is on display. This could be manual or by presenting modally. In either case you will need a done / back button to dismiss. The modal option could be done in the storyboard.
Add a delegate to the tab bar controller. Remove the nav controllers from each tab. When the tab changes, get the new view controller and set its navigationItem onto the root nav controllers nav bar.
Back button items are only shown when there is something to go back to. You can add left button items. You may be able to connect them to an unwind segue or you might need to add a method to each view controller which does:
[self.tabBarController.navigationController popViewControllerAnimated:YES];
I figured out one solution:
Objectives: When combining a Master-Detail scheme with the Detail part spreads to several scenes via tab bar controller using Push segue from Master to the tab bar scene, I want to: 1). retain the back button from the push segue, 2). set the navigation item on the tab bar scene to whichever detail scene that is visible, 3). set the right button on the detail scene. Another objective is to just use Storyboard to convey the GUI design and interaction flow, without getting into the code.
Problems: There were many problems with which controllers to use. (And one can play with the Xocde project that I uploaded here to see them.) One good solution is to use a navigation controller for each detail scene, which enables the bar button item to be added to the navigation bar on the detail scene (do not add a navigation bar to the detail scene yourself). Just from Storyboard, this worked fine, except that the navigation bar from the navigation scene would show up under the navigation bar from the tab bar scene, and the upper navigation bar wouldn't have the correct title or the correct button on it.
Solutions: The basic use of all the scenes as described in the original post is fine. For the programmatic changes part, create a view controller .h and .m for the detail scene. Then in the viewDidAppear method, retrieve the bar button that was added to the right side of the detail scene's navigation bar, and then assign it to the right bar button item on the tab bar scene's navigation bar.
The code snippet is as the follows - this is applicable to scenes that I described earlier:
UIBarButtonItem *detailButton = self.navigationItem.rightBarButtonItem;
self.parentViewController.parentViewController.navigationItem.rightBarButtonItem = detailButton;
On the Storyboard canvas, select the navigation scene, and make sure it's navigation bar is turned off.
One thing that I particularly liked abut this that the separation of the GUI design and flow are totally there.
Hope this helps.

Resources