0

I have a UITabBarController (my app's home page) using its navigationController to show a 2nd UIViewController. I want to hide the back button completely from the 2nd viewController.

In my 2nd UIViewController's viewDidLoad() I'm trying to hide the back button in many ways (and I've tried all reasonable subsets of these):

navigationItem.hidesBackButton = true
navigationItem.setLeftBarButtonItems(nil, animated: true)
navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
navigationItem.setHidesBackButton(true, animated: false)

let backButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: navigationController, action: nil)
navigationItem.leftBarButtonItem = backButton

and the text (the title of the tab bar controller) persists:

The stuck text

I don't want to jump to the conclusion that this is a bug, but might this be a bug in iOS?

I've tried all the answers found here: iOS 8: UINavigationController hide back button, but none of them have given me success.

Community
  • 1
  • 1
olympia
  • 362
  • 2
  • 20

6 Answers6

1

try this

override func viewWillAppear(animated: Bool) {
    self.tabBarController?.navigationItem.hidesBackButton = true
}

or

self.navigationItem.setLeftBarButtonItem(nil, animated: true)
  • While this code snippet may solve the question, [including an explanation](http://meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – Tobi Nary Mar 14 '16 at 11:29
0

use this code to hide back button

for Objective-C

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.hidesBackButton=YES;
}

for Swift

navigationItem.backBarButtonItem = UIBarButtonItem(title: "", style: .Plain, target: nil, action: nil)
Abhinandan Pratap
  • 2,142
  • 1
  • 18
  • 39
0

I found no answers, but I found a workaround.

I'll keep this open in hope of a better answer some day.

My solution:

Wrap the 2nd UIViewController in a new UINavigationController. Now the navigationItem.leftBarButtonItems = [<YOUR BACK BUTTON>] is successful in overriding the back button.

olympia
  • 362
  • 2
  • 20
0

All you need is to select navigationcontroller in the Xcode and goto Attributes Inspector in the right panel of xcode then In the navigation cotroller section deselect show navigation bar.enter image description here

also you have two more option in the second view controller shown bellow :

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)
option 1    navigationController?.setNavigationBarHidden(true, animated: false)
option 2    navigationController?.navigationItem.hidesBackButton = true
}
0

I don't know why, but the following method worked for me. Before loading the destination viewcontroller we should hide backbutton. This is tested in Swift 3, XCode 8, iOS 10 Simulator.

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "destinationSegue" {
            let destinationVC = segue.destination as! DestinationViewController
            destinationVC.navigationItem.setHidesBackButton(true, animated: false)
        }
    }
Adarsh V C
  • 2,314
  • 1
  • 20
  • 37
-2

Simply use the following:

self.navigationItem.hidesBackButton = true;
Karthik
  • 99
  • 2
  • 14