问题描述
我有一个带标题的导航栏.当我双击文本重命名它时,它实际上说它是一个导航项,所以可能是这样.
I have a navigation bar with a title.When I double click the text to rename it, it actually says it's a navigation item, so it might be that.
我正在尝试使用代码更改文本,例如:
I'm trying to change the text using code, like:
declare navigation bar as navagationbar here
button stuff {
navigationbar.text = "title"
}
这显然不是我的代码,只是展示了它是如何工作的.
That's not my code obviously, just showing how it would work.
所以每当我按下按钮时,我都希望标题改变.
So whenever I press the button, I want the title to change.
推荐答案
您可以通过更改 正在显示的视图控制器的标题:
viewController.title = "some title"
通常这是在视图控制器上加载视图时完成的:
Normally this is done in view did load on the view controller:
override func viewDidLoad() {
super.viewDidLoad()
self.title = "some title"
}
但是,这仅在您将视图控制器嵌入 UINavigationController 时才有效.我强烈建议您这样做,而不是自己创建导航栏.如果你坚持自己创建导航栏,你可以通过以下方式更改标题:
However, this only works if you have your view controller embedded in a UINavigationController. I highly recommend doing this instead of creating a navigation bar yourself. If you insist on creating a navigation bar yourself, you can change the title by doing:
navigationBar.topItem.title = "some title"
这篇关于以编程方式更改导航标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!