带有两个UIBarButtons的NavigationBar标题

时间:2017-03-23 14:30:48

标签: swift uinavigationcontroller uibarbuttonitem

我正在尝试设置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
}

1 个答案:

答案 0 :(得分:1)

视图控制器为导航栏设置自己的正确方法是设置自己的navigationItem

变化:

self.navigationController?.navigationBar.topItem?.title = "Title"

为:

self.title = "Title" // sets the navigationItem title

或:

navigationItem.title = "Title"