Swift-How do I add Tab Bar AND Navigation Bar to a

2019-02-01 21:13发布

问题:

Using XCode, and either through Code or through the Storyboard, can someone please explain to me how to add both a Tab Bar and Nav Bar to my view controller while keeping both visible?

Thanks so much.

回答1:

In your Storyboard, you should drag out a Tab Bar Controller and use that as the initial view controller. Then, you should embed each of the view controllers attached to the Tab Bar Controller inside Navigation Controllers (Editor menu: Embed In > Navigation Controller). Afterward, your Storyboard should look something like this:

The tab bar controller holds a tab bar and will manage switching between the other views attached to it, while the navigation controllers will place Navigation Bars at the top of each tab and help you manage navigation within the tab.



回答2:

I am assuming you want something like this. Here is how I did it in Interface Builder:

  1. Click your view controller.
  2. Editor -> Embed In -> Navigation Controller
  3. Reselect your view controller.
  4. Editor -> Embed In -> Tab Bar Controller
  5. Select the Navigation Controller
  6. Check the box next to Is Initial View Controller

Hope this helps!