Search code examples
swift3uinavigationcontrolleruinavigationbaruisplitviewcontrolleruinavigationitem

Why is title not showing in Navigation bar? Swift 3


The titles in the DetailViewController and RootController1 do not show. Also when back button is pressed in DetailViewController, it pops to RearTV instead of popping to RootController1. Can anyone tell me how to customize the titles of the navigation bar shown on UIViewControllers?

  1. Initial ViewController is of type SWRevealViewController

  2. Custom segue from SWRevealViewController to NavigationController

  3. From the Navigation Controller to RearTV (a UITableViewController with static cells holding several buttons) there is a relationship of "root view controller to RearTV"

  4. From a button on RearTV to VC2 (a UIViewController having an embed container view placed on top of it) there is "Show" segue to VC2

  5. From the container View in a VC2 there is an Embed Segue to "Split View Controller"

5.1 From the split view controller to Navigation Controller (the one to the right) there is a relationship of "master view controller to Navigation Controller"

5.1.2 From the Navigation Controller (the one to the right) to the root controller there is a relationship of "root view controller to Root Controller1"

5.1.3 From the root controller (named "Root Controller1") to Navigation Controller ("the one underneath) there is a segue "showDetail1 to Navigation Controller"

5.2 From the split view controller to navigation controller ("the one underneath) there is a relationship "detail view controller to Navigation Controller"

  1. From the navigation controller ("the one underneath) to Detail View Controller there is a segue "root view controller to Detail View Controller".

Both in RootController1 and DetailViewController I am trying to show a title, but it shows nothing.

//centers the title of the navigation bar in all view controllers
struct CustomNavBar {
static func center(title: String) -> UILabel {
    let titleLabel = UILabel(frame: CGRect(x: 0, y: 0, width: 40, height: 40))
    titleLabel.text = title
    titleLabel.adjustsFontSizeToFitWidth = true
    titleLabel.textAlignment = NSTextAlignment.center
    return titleLabel
  }
}

  override func viewDidLoad() {
      super.viewDidLoad()

     //attempt 1 to customize title of navigation bar
     self.navigationController?.navigationItem.titleView =  CustomNavBar.center(title: "some title")

     //attempt 2 to customize title of navigation bar
           self.navigationItem.titleView =  CustomNavBar.center(title: "some title")
}

//firstImage first

//secondImage second

//thirdImage third


Solution

  • Try entering the following into your view controllers:

    self.navigationController?.navigationBar.topItem?.titleView = CustomNavBar.left(title: "some title")
    

    as opposed to:

    self.navigationController?.navigationItem.titleView =  CustomNavBar.center(title: "some title")
    

    This only works when I include the Structure in the controllers.