user2636197 user2636197 - 1 year ago 146
iOS Question

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

Answer Source

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

class ViewController: UIViewController {

    @IBOutlet weak var navigationbar: UINavigationBar!

    override func viewDidLoad() {

        navigationbar.topItem!.title = "Dynamic"

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download