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

Har*_*rry 1 swift

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

Dor*_*ory 6

override func viewDidLoad() {
     super.viewDidLoad()
     self.title = "Your title over here"
}
Run Code Online (Sandbox Code Playgroud)


Joe*_*Joe 5

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)!]  
Run Code Online (Sandbox Code Playgroud)