how do you add left and right bar buttons to UIViewController in swift

244 Views Asked by At

Why are my top navbar buttons not showing up in Swift? the following is hoe i have added them and am not sure why they are not showing up when running the code

class TermsAndConditionsVC: UIViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .blue
        setBarButtons()
    }

    func setBarButtons() {
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(handleLogout))
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Logout", style: .plain, target: self, action: #selector(handleLogout))
    }

}
1

There are 1 best solutions below

1
On
    var addPostBarItem: UIBarButtonItem
    
    if #available(iOS 13.0, *) {
        addPostBarItem = UIBarButtonItem(image: .add, style: .plain, target: self, action: #selector(addPost))
    } else {
        addPostBarItem = UIBarButtonItem(title: "Add", style: .plain, target: self, action: #selector(addPost))
        // Fallback on earlier versions
    }
    navigationItem.rightBarButtonItems = [addPostBarItem]