我正在尝试设置 UINavigationBar
的标题。如果我不添加 UIBarButtons
,我可以看到标题,如果我添加它们,我不能。我两个都想看
var cancelBarButtonItem = UIBarButtonItem(
title: "Cancel".localized,
style: .plain,
target: self,
action: #selector(cancelButtonTouched(sender:))
)
var doneBarButtonItem = UIBarButtonItem(
title: "Done",
style: .plain,
target: self,
action: #selector(doneButtonTouched(sender:))
)
override func viewDidLoad() {
super.viewDidLoad()
setupRightNavBarButtons()
self.navigationController?.navigationBar.topItem?.title = "Title"
}
func setupRightNavBarButtons(){
self.doneBarButtonItem.isEnabled = false
navigationItem.leftBarButtonItem = cancelBarButtonItem
navigationItem.rightBarButtonItem = doneBarButtonItem
}
最佳答案
View Controller 为导航栏设置自己的正确方法是设置自己的 navigationItem
。
改变:
self.navigationController?.navigationBar.topItem?.title = "Title"
到:
self.title = "Title" // sets the navigationItem title
或:
navigationItem.title = "Title"
关于swift - 带有两个 UIBarButtons 的 NavigationBar 标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42979018/