0

I am trying to code without using Storyboard or Interface Builder in my project. So, in the need of create a side menu for my app, I want to create a Navigation Bar to give the chance to open the menu by tapping on the left button of my navigation Bar.

This is what I have tried with no success:

override func viewDidLoad() {
  super.viewDidLoad()
  centerViewController = ViewController()
  centerViewController.delegate = self

  centerNavigationController = UINavigationController(rootViewController: centerViewController)
  let menuButton: UIBarButtonItem = UIBarButtonItem(title: "TMDB", style: .plain, target: centerViewController, action: Selector(("toggleLeftButton")))
  centerNavigationController.navigationItem.leftBarButtonItem = menuButton
  view.addSubview(centerNavigationController.view)
  addChildViewController(centerNavigationController)

  centerNavigationController.didMove(toParentViewController: self)
}

My Navigation Bar is not showing any button nor title.

Aitor Pagán
  • 423
  • 7
  • 18
  • Pagan CHECK THIS ONCE-: http://stackoverflow.com/questions/28793331/creating-a-navigationcontroller-programatically-swift – Tushar Sharma Mar 23 '17 at 16:03
  • Thanks for answering. Not working. I have tried in my AppDelegate adding my code to generate the button too, and still not working. – Aitor Pagán Mar 23 '17 at 16:10

2 Answers2

1

I know that is no longer necessary but still. His lessons are helpfull https://www.youtube.com/watch?v=zS-CCd4xmRY

programmer
  • 99
  • 8
0

Solved

The problem was where I was writting the code. It must be instantiated inside the viewController where the NavigationController belongs.

I mean, inside the centerViewController not in his parent as I was doing.

This lines of code:

let menuButton: UIBarButtonItem = UIBarButtonItem(title: "TMDB", style: .plain, target: self, action: Selector(("toggleLeftButton")))
self.navigationItem.leftBarButtonItem = menuButton

Inside the controller ViewController.

EDIT

Reference:

Adding UIBarButtonItem to UINav..Controller

Community
  • 1
  • 1
Aitor Pagán
  • 423
  • 7
  • 18