Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide back button in navigation bar with hidesBackButton in Swift

I want to hide the back button when transitioning from one view to another. I read the questions regarding this problem and every answer was "use hidesBackButton". The problem with this is:

  • when I put it in viewDidLoad/viewWillAppear the back button arrow hides but the string "Back" doesn't.

  • when I put it in viewDidAppear the back button disappears but it visible to the user

How can I fix this?

Edit:

Here is how you can replicate this problem(or bug?)
Make a new Tabbed application with Swift in Xcode. In the FirstViewController.swift use performSegueWithIdentifier to navigate to the second view controller. In the SecondViewController.swift hide the navigation bar back button using hidesBackButton and you will see what the problem is.

like image 277
Amer Hukic Avatar asked Jan 22 '15 14:01

Amer Hukic


People also ask

How do I hide the navigation bar back button in IOS Swift?

While you are pushing 2nd controller from 1st controller, put self. navigationItem. title = "" in viewWillDisappear of 1st controller. It hides back button title from 2nd controller.

How do I hide the back button?

Way 1: Touch “Settings” -> “Display” -> “Navigation bar” -> “Buttons” -> “Button layout”. Choose the pattern in “Hide navigation bar” -> When the app opens, the navigation bar will be automatically hidden and you can swipe up from the bottom corner of the screen to show it.

How can remove back button in IOS?

To hide the back button on navigation bar we'll have to either set the navigation button as nil and then hide it or hide it directly. Let's create a project, add 2 view controller and Embed them in navigation controller.


2 Answers

To hide the back button with the latest Swift:

self.navigationItem.setHidesBackButton(true, animated: false)
like image 150
ObjectiveTC Avatar answered Oct 17 '22 19:10

ObjectiveTC


Try adding this:

let backButton = UIBarButtonItem(title: "", style: .Plain, target: navigationController, action: nil)
navigationItem.leftBarButtonItem = backButton
like image 52
ericgu Avatar answered Oct 17 '22 20:10

ericgu