How to implement tab bar controller with navigation controller in right way - ios

I am using Storyboard and Xcode 6. I have next controllers and scenes in my Storyboard:
UINavigationController that has HomeViewController as a root. HomeViewController has a button that Show (e.g. Push) UITabBarController. UITabBarController has 4 UIViewControllers.
But my problem that after I Show UITabBarController there are no Navigation Bars in 4 UIViewControllers. But I supposed that if I Show (e.g. Push) UITabBarController then it should has embedded navigation controller that is initial controller in storyboard. Am I right? And if so how can I setup then navigation bar in Storyboard, because there are now default bar event in pushed tab bar that I see on storyboard. I have selected UIViewController and set simulated metrics in identity inspector to Translucent Navigation bar for the Top property, but I supposed it should be automatically added to this controller and to the tab bar without additional steps.
Or should I add new navigation controller for each tab bar items that will have their root view controllers?
The main question why I don't see navigation bar in storyboard using show (e.g. Push). For example if I add navigation controller and then set as root - tab bar controller then Xcode automatically add top navigation bar, but if the queue has an extra step like in my case HomeViewController the top navigation bar never appear automatically.

Hi you need to embed each view controller that is within the tab bar in a navigation controller of its own. So the flow is like so (HomeVC is embedded in a NavController of it's own):
/ --> `NavController` --> `ViewController1`
| --> `NavController` --> `ViewController2`
`HomeViewController`-->`TabBarController`|--> `NavController` --> `ViewController3`
\--> `NavController` --> `ViewController4`
Go to Editor --> Embed In --> Tab Bar Controller (or Navigation Controller)
To answer your questions:
Each tab of a tab bar controller interface is associated with a custom (different [sic]) view controller. When the user selects a specific tab, the tab bar controller displays the root view of the corresponding view controller, replacing any previous views.
So the Root View Controller of the tab must be adjoined to a Navigation Controller; a navigation view controller must be next inline in order for the View Controller to inherit a Navigation. A Tab Bar switches views to whatever is next inline.
This document will help outline more information about it. https://developer.apple.com/documentation/uikit/uitabbarcontroller

In Swift 2, Xcode 7 has a very handy feature for adding a UINavigationController:
Select the UIViewController that is being used as a "tab" for the UITabBarNavigationController
On the top Xcode menu, select "Editor" ->
"Embed In" ->
"Navigation Controller"

If you want to have something like that:
TabBarController -> Navigation Controller -> View Controller with a Table View -> and from the TableView a MasterDetailView for example:
I had the problem that there were no Navigation in the MasterDetailView (no Back Button to the ViewController with The TableView).
Workaround is:
Set Segue between TableView and MasterDetailView to:
Kind: Push (Deprecated)
Run your app...hopefully you will see the Back Button...change the Kind to Show (e.g. Push), run again -> it should work.

Related

How to change the view in one tab of an UITabBar? [duplicate]

I am using Storyboard and Xcode 6. I have next controllers and scenes in my Storyboard:
UINavigationController that has HomeViewController as a root. HomeViewController has a button that Show (e.g. Push) UITabBarController. UITabBarController has 4 UIViewControllers.
But my problem that after I Show UITabBarController there are no Navigation Bars in 4 UIViewControllers. But I supposed that if I Show (e.g. Push) UITabBarController then it should has embedded navigation controller that is initial controller in storyboard. Am I right? And if so how can I setup then navigation bar in Storyboard, because there are now default bar event in pushed tab bar that I see on storyboard. I have selected UIViewController and set simulated metrics in identity inspector to Translucent Navigation bar for the Top property, but I supposed it should be automatically added to this controller and to the tab bar without additional steps.
Or should I add new navigation controller for each tab bar items that will have their root view controllers?
The main question why I don't see navigation bar in storyboard using show (e.g. Push). For example if I add navigation controller and then set as root - tab bar controller then Xcode automatically add top navigation bar, but if the queue has an extra step like in my case HomeViewController the top navigation bar never appear automatically.
Hi you need to embed each view controller that is within the tab bar in a navigation controller of its own. So the flow is like so (HomeVC is embedded in a NavController of it's own):
/ --> `NavController` --> `ViewController1`
| --> `NavController` --> `ViewController2`
`HomeViewController`-->`TabBarController`|--> `NavController` --> `ViewController3`
\--> `NavController` --> `ViewController4`
Go to Editor --> Embed In --> Tab Bar Controller (or Navigation Controller)
To answer your questions:
Each tab of a tab bar controller interface is associated with a custom (different [sic]) view controller. When the user selects a specific tab, the tab bar controller displays the root view of the corresponding view controller, replacing any previous views.
So the Root View Controller of the tab must be adjoined to a Navigation Controller; a navigation view controller must be next inline in order for the View Controller to inherit a Navigation. A Tab Bar switches views to whatever is next inline.
This document will help outline more information about it. https://developer.apple.com/documentation/uikit/uitabbarcontroller
In Swift 2, Xcode 7 has a very handy feature for adding a UINavigationController:
Select the UIViewController that is being used as a "tab" for the UITabBarNavigationController
On the top Xcode menu, select "Editor" ->
"Embed In" ->
"Navigation Controller"
If you want to have something like that:
TabBarController -> Navigation Controller -> View Controller with a Table View -> and from the TableView a MasterDetailView for example:
I had the problem that there were no Navigation in the MasterDetailView (no Back Button to the ViewController with The TableView).
Workaround is:
Set Segue between TableView and MasterDetailView to:
Kind: Push (Deprecated)
Run your app...hopefully you will see the Back Button...change the Kind to Show (e.g. Push), run again -> it should work.

Why does embedding a View Controller in a Navigation Controller, then in a Tab Bar Controller attach the nav bar to the Tab Bar Controller?

Back when I first created the foundational layout for the app I'm currently working on in Storyboard, I did it in two steps:
Selected my View Controller and used Editor->Embed In->Navigation Controller.
Selected my View Controller again and used Editor->Embed In->Tab Bar Controller.
This was the resulting layout:
Question 1: Why do these steps create such an odd layout?
In this weird layout that seems to imply that the Navigation Controller is attached to the Tab Bar Controller as its parent, the only way I can get navigation items to display in the app (in the view controlled by the View Controller on the right) is by placing them in the nav bar of the Tab Bar Controller scene.
However, this creates various issues, including not being able to create an IBOutlet in my View Controller file for a custom view I drag into the title view slot in the Tab Bar Controller scene. Meanwhile, dragging anything into the navigation bar in the View Controller scene just makes it not appear in the app when it runs.
Question 2: How can I fix this layout so that I can control-drag from navigation items into my View Controller file? Or is everything actually correct, and I'm just trying to force something I shouldn't? (My intention here is to be able to set the custom title view's text in my View Controller code.)
Its obvious, if you want to embed MyViewController to NavigationController then you need to change your second step and need to embed NavigationController to Tab Bar Controller.
Selected MyViewController and used Editor->Embed In->Navigation Controller.
Selected NavigationController Embed with MyViewController and used Editor->Embed In->Tab Bar Controller.
It should look like this.
Note: You need to embed MyViewController to navigationController only if you want to perform push operation on this TabBarItem means with MyViewController other wise simply embed MyViewController to TabBarController no need to embed it with NavigationController.

Navigation controller back button doesnt appear?

I'm trying to setup back button on the navigation controller but it doesn't appear when I run it. I watched some tutorials and it usually works.
Your flow should be:
NavigationController --> MainVC --> FoodVC
Now you can add button as follow:
Navigation Item -> Left Bar Button Items -> Bar Button Item -> back button
Go to navigation controller scene -> navigation controller ->navigation bar - then go to right top show attributes option and change bar tint color.Try it
The view controller food is embedded in Navigation Controller , hence it is root view controller.
Navigation bar will appear when any viewController is push on same navigation controller.
So, If you tableview is already in Navigationcontroller then remove Navigation Controller in which Your FoodVC Controller is embedded.
If you want that navigation controller, you may add leftBarButtonItem programatically.
Totally depends on your requirement.
Is MenuVc your main Controller? try this, delete that navigation controller in the middle. Then click the MenuVc, embed in Navigation Controller and set the navigation controller as initial view controller at the attribute inspector. then navigate the menuVC to Food, "Show".

Tab Bar Item disappears after adding Navigation Controller

First I created a VC with TabBarController like this.
The other tabs and the main controller are at other storyboards.
Then, I added the Navigation Controller from the Editor > Embed In > Navigation Controller.
And the TabBarItem immediately disappears.
How can I change my tab bar icons and names?
In your storyboard you can change the tab icon from the Navigation controller. (Provided you have different navigation controllers for different tabs of your tab bar controller). YOUR NC will look something like this. (NC -> VC)
I checked your attach images. There are no issue with your navigation controller it will work fine with your tab bar.

In swift: how to add tab bar icons to a regular ViewController storyboard?

I would like to add two tab bar icons (table view and collection view), as shown in the first figure below, to a regular ViewController storyboard (as shown in the second figure).
I have tried to embed the ViewController in the TabBarController (Editor -> Embed in -> Tab Bar Controller), but it seems to me that xcode only allows me to add one icon (Tab Bar Item).
You need one view controller per tab in the storyboard, then connect the tab view controller to those controllers using "relationship segue", "view controllers" (ctrl-drag from tab view controller to destination controllers, then select that).
In each controller you then can click the tab item and set an image, in attributes.
add tab bar item to the view controller u want to add in tab bar controller. and then control drag from tab bar view controller and the make "relationship segue as "view controllers"..

Resources