Changing navigation title programmatically

后端 未结 14 2239
长情又很酷
长情又很酷 2020-11-28 19:34

I have a navigation bar with a title. When I double click the text to rename it, it actually says it\'s a navigation item, so it might be that.

I\'m trying to change

14条回答
  •  无人及你
    2020-11-28 19:43

    You change the title by changing the title of the view controller being displayed:

    viewController.title = "some title"
    

    Normally this is done in view did load on the view controller:

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "some title"
    }
    

    However, this only works if you have your view controller embedded in a UINavigationController. I highly recommend doing this instead of creating a navigation bar yourself. If you insist on creating a navigation bar yourself, you can change the title by doing:

    navigationBar.topItem.title = "some title"
    

提交回复
热议问题