Mister Mister Mister Mister - 1 year ago 78
Swift Question

UINavigationController hides back button only on second appearance of view

I have three view controllers: A login view, a home tableview screen, and a detail tableview screen. These are all part of the same UINavigationController. On the home screen, I want to disable the back button, preventing a user from backing into the login screen. So in the

function I have

// Home View
override func viewDidLoad() {
self.navigationItem.hidesBackButton = true

When this view shows up, there is no back arrow but there is the word "Back." This button does nothing, it seems disabled. When I go to the next view (the detail view), the back button is hidden. Even if I add a
hidesBackButton = false
to the
of this view, still nothing. However, the swipe to the right on the navigation bar allows going back.

Now that I am back on the home screen, the back button is completely hidden. If I were to then proceed forward again to the detail view, the back button appears as desired.

Here's the Sequence of Events:

  1. Login Screen. After you log in the home screen is popped on.

First Login Screen

  1. The Home Screen. The back button is there, where it shouldn't.

Back Button appears without arrow

  1. The detail view. This view should have a back button to go back to the home screen. The swipe functionality still works, so you can go back to the home screen by swiping the navigation bar.

Detail Test View. Should have Back button

  1. When you've gone back, the back button is gone! Like it should be.

Back To Home.

  1. Finally, going back (forward) to the detail view, the back button is there as it should be.

Back to Detail

Answer Source

On the simulator I reset content and settings, and this seemed to fix the issue.

I still have no idea why the bug was happening in the first place.

Real solution

This solution actually seems to be working for me!

I simply added

var HVC = segue.destinationViewController as! HomeViewController
HVC.navigationItem.setHidesBackButton(true, animated: false)

to the prepareForSegue method in the ViewController that created the problematic VC.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download