Amer Hukic
Amer Hukic

Reputation: 1524

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:

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.

Upvotes: 40

Views: 54842

Answers (9)

Sir Papilonius
Sir Papilonius

Reputation: 113

In XCode 11(maybe sooner, not sure), you can also untick the box under the attribute inspector tab in the storyboard editor if you're not looking to do it programatically.

Upvotes: 0

ericgu
ericgu

Reputation: 2249

Try adding this:

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

Upvotes: 52

Joe M
Joe M

Reputation: 677

Worked for me when I set it in init(), instead of viewDidLoad. Strange though

Upvotes: 1

luhuiya
luhuiya

Reputation: 2211

this worked for me

navigationController?.navigationBar.topItem?.hidesBackButton = true

Upvotes: 14

Celil Bozkurt
Celil Bozkurt

Reputation: 1783

You can use the code below to hide back button on UINavigationBar.

Swift 3;

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

Upvotes: 19

Ha cong Thuan
Ha cong Thuan

Reputation: 683

Try adding this,This worked for me

navigationItem.hidesBackButton = true

Upvotes: 6

Alvin George
Alvin George

Reputation: 14296

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.

    self.navigationController?.navigationBarHidden = false
    var button: UIButton = UIButton()
    button.setImage(UIImage(named: "person-icon.jpg"), forState: .Normal)
    button.frame = CGRectMake(0, 0, 25, 25)
    button.targetForAction("actioncall", withSender: nil)
    var rightItem:UIBarButtonItem = UIBarButtonItem()
    rightItem.customView = button
    self.navigationItem.rightBarButtonItem = rightItem

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

override func viewWillAppear(animated: Bool) {
    let backButton = UIBarButtonItem(title: "", style: UIBarButtonItemStyle.Plain, target: navigationController, action: nil)
    navigationItem.leftBarButtonItem = backButton
}

Upvotes: 7

ObjectiveTC
ObjectiveTC

Reputation: 2507

To hide the back button with the latest Swift:

self.navigationItem.setHidesBackButton(true, animated: false)

Upvotes: 60

Bruno Cunha
Bruno Cunha

Reputation: 1810

This worked for me:

override func viewWillAppear(animated: Bool)
{
    super.viewWillAppear(animated)

    self.tabBarController?.navigationItem.hidesBackButton = true
}

Upvotes: 3

Related Questions