UINavigationBar - Set title programmatically?

前端 未结 11 1240
伪装坚强ぢ
伪装坚强ぢ 2020-12-24 04:24

I have a tab bar application with a different view on each tab. Each view has a UINavigationBar with title set on Interface Builder. I want to change the title based on a cl

相关标签:
11条回答
  • 2020-12-24 04:57

    Use

    self.navigationItem.title = @"the title";
    

    as setting navBar.topItem.title will not work in all circumstances.

    0 讨论(0)
  • 2020-12-24 05:03

    Create IBOutlet of UINavigationBar

    navigationBar.topItem.title = @"Title";
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-24 05:05

    I achieved this way in Swift 5

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationItem.title = "Title"
    }
    
    0 讨论(0)
  • 2020-12-24 05:07

    You can also just set the title in the ViewDidLoad under your ViewController or TableViewController using

    _title = @"Title";
    

    or

    self.title = @"Title";
    
    0 讨论(0)
  • 2020-12-24 05:11

    I used the following:

    self.navigationController.navigationBar.topItem.title = @"Title";
    

    However, I also had to call it in a dispatch block as it wouldn't change the title when called within the viewDidLoad w/o it.

    0 讨论(0)
  • 2020-12-24 05:11

    Note that in order for the title to show up AT ALL the Navigation Controller's delegate MUST be set to the navigation bar itself or the title will NEVER show!

    0 讨论(0)
提交回复
热议问题