如何在swift中以编程方式更改导航栏标题?

时间:2017-01-17 06:54:23

标签: swift

我没有使用导航控制器,而是使用导航栏。但我无法找到任何以编程方式更改标题的解决方案。请快速提供解决方案。

4 个答案:

答案 0 :(得分:5)

override func viewDidLoad() {
     super.viewDidLoad()
     self.title = "Your title over here"
}

答案 1 :(得分:4)

viewDidLoad 中尝试以下代码。

// To Set your navigationBar title.
yourNavBarName.topItem?.title = "Some Title"

// To Set your navigationBar backgound.
yourNavBarName.barTintColor = .red 

// To Set your navigationBar title font and color.
yourNavBarName.titleTextAttributes = [NSForegroundColorAttributeName:UIColor.green, NSFontAttributeName:UIFont(name:"HelveticaNeue", size: 26)!]  

答案 2 :(得分:1)

Swift 4

这将更改导航栏堆栈的顶部项目

navigationController?.navigationBar.topItem?.title = "TEST"

答案 3 :(得分:1)

要更改特定视图控制器中的标题,请使用以下命令:

navigationItem.setCustomTitle("Title", font: UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.medium), textColor: .white)

如果您从具有标题的其他导航控制器推送,则不必将其更改回来。