Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS rightBarButtonItem on UINavigationController in swift

Tags:

I'm trying to put a rightBarButtonItem on a second view controller of an UINavigationViewController stack.

I'm creating and setting the button in viewDidLoad of the view controller that I want to show. My actual code looks like this:

override func viewDidLoad() {     super.viewDidLoad()     menu_button_ = UIBarButtonItem(image: UIImage(named: "menu"),         style: UIBarButtonItemStyle.Plain ,         target: self, action: "OnMenuClicked:")      self.navigationController!.navigationItem.rightBarButtonItem = menu_button_ } 

What am I missing? The button doesn't appear.

like image 542
Marcone Avatar asked Jun 23 '15 03:06

Marcone


1 Answers

You should set the menu_button_ as the rightBarButtonItem of your viewController rather than the navigationController.

Try

self.navigationItem.rightBarButtonItem = menu_button_ 

instead of

self.navigationController!.navigationItem.rightBarButtonItem = menu_button_ 
like image 173
liushuaikobe Avatar answered Oct 19 '22 23:10

liushuaikobe