Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift Navigation Bar Button and Title don't appear

I'd like to create a Navigation bar on top of my App. I created an Navigation Controller -> Tab Bar Controller -> Navigation Controller -> Table Controller

  1. I dragged a Bar Button Item on the upper right side.
  2. I double clicked the title in the middle of the Table Controller and wrote a text.
  3. I also tried it with this code in the viewDidLoad() of my controller:

    self.navigationController.navigationBar.topItem.title = "some title"
    self.navigationItem.setRightBarButtonItem(UIBarButtonItem(barButtonSystemItem: .Search, target: self, action: "barButtonItemClicked:"), animated: true)
    self.navigationItem.title = "YourTitle"
    

Non of it worked - what am I doing wrong? :/

like image 539
Joe Avatar asked Aug 20 '14 09:08

Joe


3 Answers

You can try something like this:

self.title = "Your Title"

var homeButton : UIBarButtonItem = UIBarButtonItem(title: "LeftButtonTitle", style: UIBarButtonItemStyle.Plain, target: self, action: "")

var logButton : UIBarButtonItem = UIBarButtonItem(title: "RigthButtonTitle", style: UIBarButtonItemStyle.Plain, target: self, action: "")

self.navigationItem.leftBarButtonItem = homeButton
self.navigationItem.rightBarButtonItem = logButton
like image 144
Va Visal Avatar answered Nov 06 '22 14:11

Va Visal


I believe you are looking for a titleView.

Try:

var navBarTitleView = UIView(frame: CGRectMake(0.0, 0.0, self.view.frame.width, 44.0))
navBarTitleView.backgroundColor = UIColor.brownColor()
self.navigationItem.titleView = navBarTitleView
like image 44
Katafalkas Avatar answered Nov 06 '22 15:11

Katafalkas


I am pretty sure that you can just set the title of your table controller, in its viewDidLoad, which is the title displayed in your navigation bar.

this answer explains a slightly different question, but the concept is the same:

https://stackoverflow.com/a/25167491/2070902

like image 31
robotwholearned Avatar answered Nov 06 '22 13:11

robotwholearned