我没有使用导航控制器而是导航栏。但是我找不到任何以编程方式更改标题的解决方案。请快速提供解决方案。
override func viewDidLoad() {
super.viewDidLoad()
self.title = "Your title over here"
}
在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)!]
斯威夫特4
这将更改导航栏堆栈的顶部项目
navigationController?.navigationBar.topItem?.title = "TEST"
要更改特定View控制器中的标题,请使用以下命令:
navigationItem.setCustomTitle("Title", font: UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.medium), textColor: .white)
这样,如果您从另一个具有标题的导航控制器推送,则无需更改它。