我没有使用导航控制器,而是使用了一个导航栏。但是我找不到任何编程方法来更改标题。请提供一个Swift解决方案。
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)!]
Swift 4
这将改变导航栏堆栈中顶部项目的内容
navigationController?.navigationBar.topItem?.title = "TEST"
要更改特定视图控制器中的标题,请使用以下代码:
navigationItem.setCustomTitle("Title", font: UIFont.systemFont(ofSize: 16, weight: UIFont.Weight.medium), textColor: .white)
这样,如果您从具有标题的另一个导航控制器推送,就不必将其改回来。