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

问题描述 投票:1回答:4

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

swift
4个回答
5
投票
override func viewDidLoad() {
     super.viewDidLoad()
     self.title = "Your title over here"
}

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)!]  

1
投票

斯威夫特4

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

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

1
投票

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

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

这样,如果您从另一个具有标题的导航控制器推送,则无需更改它。

© www.soinside.com 2019 - 2024. All rights reserved.