我正在尝试设置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
}
答案 0 :(得分:1)
视图控制器为导航栏设置自己的正确方法是设置自己的navigationItem
。
变化:
self.navigationController?.navigationBar.topItem?.title = "Title"
为:
self.title = "Title" // sets the navigationItem title
或:
navigationItem.title = "Title"