How to add a button to UINavigationBar?

后端 未结 7 1735
暖寄归人
暖寄归人 2020-12-02 06:19

How to add a button to UINavigationBar programmatically?

7条回答
  •  有刺的猬
    2020-12-02 06:52

    In Swift 2, you would do:

    let rightButton: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.Done, target: nil, action: nil)
    self.navigationItem.rightBarButtonItem = rightButton
    

    (Not a major change) In Swift 4/5, it will be:

    let rightButton: UIBarButtonItem = UIBarButtonItem(title: "Done", style: UIBarButtonItem.Style.done, target: nil, action: nil)
    self.navigationItem.rightBarButtonItem = rightButton
    

提交回复
热议问题