Search code examples
swiftuinavigationbarsegue

NavigationBar.titleTextAttributes are nil


I have 2 view controllers. Now I want to make a present-segue from one to the other. I made the segue in the storyboard, but performed it in the source code of the first vc. The view which will be shown has this line in the viewDidLoad method:

var font = self.navigationController?.navigationBar.titleTextAttributes![NSAttributedString.Key.font] as! UIFont

When the view loads in the segue there is an exception and I checked that the titleTextAttributes are nil. When I use the show-segue it works, but I don't want this back-button in my navigation bar.

What is my mistake or is there any other solution?


Solution

  • The problem is that, when you use present, you are simply presenting a ViewControllerB Over a ViewControllerA(Embedded in NavigationController), But as ViewControllerB is having different NavigationController or may not have Navigation Controller, when you present, so you are getting nil there

    You can simply remove that line of code and use this

    var font = UIFont(name: "Your Font name", size: YOUR_FONT_SIZE)
    

    And do present as your are doing with Segue.

    Hope it helps