Search code examples
iosswiftxcodeuinavigationcontrolleruinavigationitem

swift ios cant change navigation bar title


I have a modal VC and I have dragged in a navigation bar using storyboard:

enter image description here

But now I can only change the title in storyboard and not in code. I need to be able to change it in code since the title will be dynamic.

I have tried both of these:

self.title = "some title"

navigationBar.topItem.title = "some title"

But none of them work and I dont get any warning/error either


Solution

  • If your NavigationBar is connected to your ViewController per Outlet, this works fine:

    class ViewController: UIViewController {
    
        @IBOutlet weak var navigationbar: UINavigationBar!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            navigationbar.topItem!.title = "Dynamic"
        }
    
    }