M. Tol
M. Tol

Reputation: 65

hidesBackButton is not working, even though in viewDidLoad

I have seen similar questions, but those questions are all very old. I'm trying to hide the 'back' button in my ViewController. Now, it looks like this: https://www.dropbox.com/s/ipmbqgxji574j06/Screenshot%202020-07-21%2020.25.17.png?dl=0

My code (I've tried implementing it in both viewWillAppear & viewDidLoad, neither seem to work):

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
    
    // Setting up title of ViewController
    self.navigationController?.isNavigationBarHidden = false
    self.navigationController?.navigationBar.topItem?.title = "Groceries"
    self.navigationController?.navigationBar.prefersLargeTitles = true
    self.navigationItem.hidesBackButton = true
}

override func viewDidLoad() {
    super.viewDidLoad()
    self.navigationItem.setHidesBackButton(true, animated: true)
    print(self.navigationItem.hidesBackButton)
    // Returns true
}

I have also tried putting the code in prepareForSegue:

override func prepare(for segue: UIStoryboardSegue, sender: Any?){
    if segue.identifier == K.welcomeSegue{
    let objVC = segue.destination as? TableViewController
    objVC?.navigationItem.hidesBackButton = true
  }
}

Could anyone help me?

Thanks!

Upvotes: 2

Views: 1200

Answers (1)

Harish
Harish

Reputation: 2512

To hide the backButton on navigationBar either set the navigationButton as nil and then hide it or hide it directly.

A combination of these to approaches would be a better solution and works even if you have set a custom navigationBar.

self.navigationItem.leftBarButtonItem = nil
self.navigationItem.hidesBackButton = true

You can also use

override func viewDidLoad() {
   super.viewDidLoad()
   self.navigationItem.setHidesBackButton(true, animated: false)
}

Upvotes: 4

Related Questions