0

I'm trying to achieve what I think would be a simple task, but despite similar posts on here being answered, the solution eludes me....

I'm using Main.storyboard in Xcode 8/swift 3 to create an application with the initial ViewController being a UINavigationController. I then want to push to UITabBarController which has two view controllers which it holds a relationship with:

override func viewDidLoad() {
    super.viewDidLoad()

    let mainStoryboard = UIStoryboard(name: "Main", bundle: nil)
    if let vc = mainStoryboard.instantiateViewController(withIdentifier: "test") as? UITabBarController {
        self.navigationController?.pushViewController(vc, animated: true)
    }
}

When launching the app, the initial ViewController successfully 'pushes' to the TabBarController/it's ViewControllers (image below). The issue I'm having is after adding navigation items/buttons in the TabBarController ViewControllers (either in Storyboard or programmatically) the buttons / nav items never show.

Storyboard setup

Simulator screenshot

I have seen a few posts such as the below links and have followed the suggested steps verbatim, but nothing has solved the problem. Any help would be greatly appreciated!

Adding buttons to navigation controllers How to add buttons to navigation controller visible after segueing?

Yerken
  • 299
  • 1
  • 12
lhammer
  • 15
  • 4

1 Answers1

1

It's does not show, because your TabBarController also have his own UINavigationBar. ViewControllers are inside TabBarController

you can create custom TabBarController and handle tabs actions Try this code:

class TabBarController: UITabBarController {
  override var selectedViewController: UIViewController? {
    didSet {
      switch self.selectedViewController {
      case self.selectedViewController is FirstViewController:
        self.navigationItem.rightBarButtonItem = self.firstButton
      case self.selectedViewController is SecondViewControlller:
        self.navigationItem.rightBarButtonItem = self.secondButton
      default:
        break
      }
    }
  }
}
Yerken
  • 299
  • 1
  • 12