Swift 如何设置导航栏的标题?
How to set title of Navigation Bar in Swift?
我正在尝试在 Swift 中设置 Navigation Bar
的标题,我设置了 Tab Bar
并且在 Navigation Bar
中没有显示任何内容,没有按钮,没有标题,什么都没有.我使用了一些代码,但在我使用 Tab Bar
时它不工作,当我删除 Tab Bar
时,代码工作正常并且 Navigation Bar
一切正常,显示标题和按钮。
我用于标题的代码是:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "My Title"
}
然后在 Main.storyboard
中,我将 Navigation Controller
连接到 Tab Bar Controller
,如图所示。
那么,如何解决这个问题?问题是 Navigation Bar
在使用 Tab Bar
.
时不起作用
您的问题不清楚,如果您提供的代码来自 UIViewController
,保存在 UINavigationController
中,并且显示了 navigationBar
,您可以简单地使用:
override func viewDidLoad() {
super.viewDidLoad()
self.title = "My Title"
}
由于 Tab Bar
实际上是 Navigation Bar
的 Root View Controller
,您需要在 [=] 中设置 UITabBarController
的 title
16=] 功能,以便每次切换标签时都会发生:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.navigationItem.title = "My Title"
self.tabBarController?.navigationItem.leftBarButtonItem = settingsButton //This is the IBOutlet variable that you previously added
}
但更好的方法实际上是反过来,就像这样
您应该为 UITabBarController
的每个 child 连接一个 UINavigationController
,因为它在语义上更正确,并且更易于维护。
在 UINavigationController Class 参考资料 Apple 中这样写道:
init(rootViewController: UIViewController)
Parameters
rootViewController
The view controller that resides at the bottom of the navigation stack. This object cannot be an instance of the UITabBarController class.
但是如果你insisted.But在这种情况下,你可以做到这一点,你的 tabbarController 的 ViewControllers 都共享相同的标题,这是 tabbarController 的标题。
更好的方法是给每个 tabbarController 的 viewController 一个 NavigationViewController。
我正在尝试在 Swift 中设置 Navigation Bar
的标题,我设置了 Tab Bar
并且在 Navigation Bar
中没有显示任何内容,没有按钮,没有标题,什么都没有.我使用了一些代码,但在我使用 Tab Bar
时它不工作,当我删除 Tab Bar
时,代码工作正常并且 Navigation Bar
一切正常,显示标题和按钮。
我用于标题的代码是:
override func viewDidLoad() {
super.viewDidLoad()
self.navigationItem.title = "My Title"
}
然后在 Main.storyboard
中,我将 Navigation Controller
连接到 Tab Bar Controller
,如图所示。
那么,如何解决这个问题?问题是 Navigation Bar
在使用 Tab Bar
.
您的问题不清楚,如果您提供的代码来自 UIViewController
,保存在 UINavigationController
中,并且显示了 navigationBar
,您可以简单地使用:
override func viewDidLoad() {
super.viewDidLoad()
self.title = "My Title"
}
由于 Tab Bar
实际上是 Navigation Bar
的 Root View Controller
,您需要在 [=] 中设置 UITabBarController
的 title
16=] 功能,以便每次切换标签时都会发生:
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
self.tabBarController?.navigationItem.title = "My Title"
self.tabBarController?.navigationItem.leftBarButtonItem = settingsButton //This is the IBOutlet variable that you previously added
}
但更好的方法实际上是反过来,就像这样
您应该为 UITabBarController
的每个 child 连接一个 UINavigationController
,因为它在语义上更正确,并且更易于维护。
在 UINavigationController Class 参考资料 Apple 中这样写道:
init(rootViewController: UIViewController)
Parameters rootViewController
The view controller that resides at the bottom of the navigation stack. This object cannot be an instance of the UITabBarController class.
但是如果你insisted.But在这种情况下,你可以做到这一点,你的 tabbarController 的 ViewControllers 都共享相同的标题,这是 tabbarController 的标题。
更好的方法是给每个 tabbarController 的 viewController 一个 NavigationViewController。