You want to set the .rightBarButtonItems
property. You can change this as needed.
So, for example, we'll start with only the +
button showing, and when it is tapped we'll also show the Edit
button:
class TestViewController: UIViewController {
lazy var addButton: UIBarButtonItem = {
let b = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(self.myAddAction(_:)))
return b
}()
lazy var editButton: UIBarButtonItem = {
let b = UIBarButtonItem(title: "Edit", style: .plain, target: self, action: #selector(self.myEditAction(_:)))
return b
}()
@objc func myAddAction(_ sender: Any) -> Void {
print("Add button tapped!")
// this will add both buttons
self.navigationItem.rightBarButtonItems = [editButton, addButton]
}
@objc func myEditAction(_ sender: Any) -> Void {
print("Edit button tapped!")
}
override func viewDidLoad() {
super.viewDidLoad()
// let's start with only the "add" button,
// and show the "edit" button after the add button has been tapped
self.navigationItem.rightBarButtonItems = [addButton]
}
}