1
votes

I thought this would be a simple thing to do, but I'm having some issues with the title even showing up. I have a TabBarController, that manages 2 ViewControllers. The tabBarController is being presented view the show segue from a tableView that is embedded into a Navigation Controller. I've read different view points as to if this is correct or not. It seems to make sense from a UI view for me. Person clicks on table cell and is presented with more detail information about the table cell in a TabBar Controller View.

When I test the app, I don't see any title at all, even though I added one in the storyboard. I also added a nav button on the right side of the nav bar that doesn't show up at all either. See attached image.

To set the title I was trying to do something like this

 self.title = "Some Title Name"

But again my title nor my button even shows up when testing the app. When I use the line of code above it changes the title of the tab that you tab on, not the title of the view controller.

Could this have something to do with the tabBar being part of the navigation controller, and therefore the controller is the one creating and populating the title bar, such as the back button that I can see. If this is the case, am I able to add a title and a button on the right side of the nav bar? enter image description here enter image description here

enter image description here

2
navigationItem.title = "Some Title Name" - bubuxu
This changes the title of the Tab ( the button at the bottom of the screen) - icekomo

2 Answers

4
votes

When you do self.title = "Some title", you are setting the title of the ViewController, which is working fine, as you can because the tab bar title changes. But that is different from the title of a navigation bar.

By default the ViewController of your tab bar does NOT have a navigation bar, you need to either embed the view controller in a navigation controller, or you could drag and drop a navigation bar from storyboard elements.

If the navigation bar comes from embedding the View Controller in a navigation controller, then you can do self.navigationItem.title = "Some title" and it would work.

But if you got the navigation bar by dragging an UI element onto the view controller. In that case you would have to connect that navigation bar as an IBOutlet and change it.

@IBOutlet weak var navBar: UINavigationBar!

override func viewDidLoad() {
    super.viewDidLoad()
    self.navBar.topItem?.title = "Some Title"
}

Regarding the navigation bar now showing up, did you try adding constraints to it?

0
votes

Here is what I used to solve these issue:

To get the title to show up:

tabBarController?.title = "Your Title" tabBarController?.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Right Button Title", style: UIBarButtonItemStyle.Plain, target: self, action: "rightButtonFunction")

To use an icon on the right side of the navigation bar

let button = UIButton.init(type: .custom) button.setImage(UIImage.init(named: "pencilIcon.pdf"), for: UIControlState.normal) button.addTarget(self, action:#selector(DetailsViewController.callMethod), for: UIControlEvents.touchUpInside) button.frame = CGRect.init(x: 0, y: 0, width: 30, height: 30) //CGRectMake(0, 0, 30, 30) let barButton = UIBarButtonItem.init(customView: button) tabBarController?.navigationItem.rightBarButtonItem = barButton