0

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))
    }

}
Gino Sesia
  • 383
  • 2
  • 4
  • 14
  • Is your view controller inside a navigation view controller? – EmilioPelaez Aug 31 '20 at 16:37
  • yes i think it is – Gino Sesia Aug 31 '20 at 16:42
  • 1
    Make sure that your View Controller is inside a UINavigationController. An easy way to test this is print(navigationController?) in viewDidLoad or stopping at a breakpoint and typing 'po navigationController' into the console and making sure it isn't nil. – Nate4436271 Aug 31 '20 at 17:46
  • 1
    Your code posted looks good. @Nate4436271 has some good ways to check if there is a UINavigationController too. Two things that could help us help you... `TermsAndConcitionsVC` - how it it being shown? Many times this is presented modally as a popup (and naming your button titles "Logout" suggests it is). Second, a navigation controller needs to be explicitly created. Either in code or in a storyboard. (There are other ways but these are the two most-used ways.) Simply replying "yes, i think it is" implies you don't know. Not to sound rude, but you really should know. –  Aug 31 '20 at 18:03
  • In an effort that I hope was helpful I have uploaded a test project. Your code seems to work for me. In the test project I have set up a ViewController embedded in a NavigationController through a storyboard and code. https://github.com/natehancock/SetBarButtonItems – Nate4436271 Aug 31 '20 at 18:17
  • Thank you for the help i realised i had commented out a line that i needed when presenting it, now everything is working again – Gino Sesia Sep 01 '20 at 10:22

1 Answers1

-2
    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]
Roman Ryzhiy
  • 1,540
  • 8
  • 5
  • 1
    First, posting code with no explanation is really not a good idea. Second, why is this an improvement over what was posted in the question? Because it uses an SF Symbols icon if available? Finally, how is this an actual *answer* to the question? That's why I'm down-voting your answer. –  Aug 31 '20 at 17:57