135

Right now I have two view controllers. My problem is I don't know how to hide the back button after transitioning to the second view controller. Most references that I found are in Objective-C. How do I code it in Swift?

Hide back button code in Objective-C

[self.navigationItem setHidesBackButton:YES animated:YES];
shim
  • 9,289
  • 12
  • 69
  • 108
Nurdin
  • 23,382
  • 43
  • 130
  • 308

13 Answers13

463

According to the documentation for UINavigationItem :

self.navigationItem.setHidesBackButton(true, animated: true)
Sourabh Sharma
  • 8,222
  • 5
  • 68
  • 78
Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • 38
    the snarkiness is for free. – Greg Sep 29 '15 at 00:23
  • 77
    It might frighten you @Paulw11, but the plain fact is typing "Swift hide back button" into Google and coming here - like I just did - is *tons* quicker than ploughing through the reams of text in the official documentation. – Joseph Beuys' Mum Dec 21 '15 at 07:29
  • 8
    Whereas the name of the back button is defined in the first view controller, it may be interesting to notice that the code to hide it must be implemented in the second view controller (the one showing the button). – XLE_22 Sep 15 '17 at 21:31
53

In case you're using a UITabBarController:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.tabBarController?.navigationItem.hidesBackButton = true
}
shim
  • 9,289
  • 12
  • 69
  • 108
Bruno Cunha
  • 1,680
  • 1
  • 17
  • 15
  • Same for me, however I would like to understand why. Does anyone knows what is the difference between the accepted answer and this approach? The only thing I can think of is the inheritance model vs a static version of the navigation bar. – Helmut Granda Mar 03 '16 at 05:47
  • 1
    @HelmutGranda If you notice that Bruno is using a Tab Bar Controller, that is the difference. In the accepted answers' case, the VC is likely embedded in a navigation controller. – cloudcal Dec 12 '16 at 08:19
  • @cloudcal I'm not using a tabBar, yet both answers worked. Both of them hid the back button. I thought they'd behave different if the viewController was *on the screen* and then I wanted to hide the back button. Yet again they both acted the same. I wasn't able find any difference between them. I'm saying Bruno's answer is incorrect. Rather for pure navigationController it doesn't make a difference. I haven't tried tabBarController – mfaani Jan 07 '20 at 20:56
  • ```override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.tabBarController?.navigationItem.hidesBackButton = true }``` – tae ha Nov 26 '22 at 02:28
26

Swift

// remove left buttons (in case you added some)
 self.navigationItem.leftBarButtonItems = []
// hide the default back buttons
 self.navigationItem.hidesBackButton = true
Marwen Doukh
  • 1,946
  • 17
  • 26
  • It's clear that navigationItem.hidesBackButton = true is the right answer. If there are left buttons, which can be the case, then this is most definitely the right answer, and should be considered the more complete answer. – truedat101 Aug 14 '18 at 06:40
  • `self.navigationItem.hidesBackButton = true` Its working as expected. – Shahabuddin Vansiwala Jul 18 '19 at 09:01
  • In my case with several navigationControllers only this helped: **self.navigationItem.leftBarButtonItems = []** – J A S K I E R Oct 18 '20 at 09:36
10

This is also found in the UINavigationController class documentation:

navigationItem.hidesBackButton = true
Amiru Homushi
  • 139
  • 1
  • 5
9

Put it in the viewDidLoad method

navigationItem.hidesBackButton = true 
Rikh
  • 4,078
  • 3
  • 15
  • 35
Harjeet Singh
  • 91
  • 1
  • 2
5

In SwiftUI

.navigationBarBackButtonHidden(true)
Pang
  • 9,564
  • 146
  • 81
  • 122
4
self.navigationItem.setHidesBackButton(true, animated: false)
Pang
  • 9,564
  • 146
  • 81
  • 122
Vinay Podili
  • 335
  • 1
  • 3
  • 16
4

Try with below code in viewWillAppear method.

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

Read below link for more support. https://developer.apple.com/documentation/uikit/uinavigationcontroller/customizing_your_app_s_navigation_bar

Priyank Patel
  • 791
  • 8
  • 6
3

That worked for me in Swift 5 like a charm, just add it to your viewDidLoad()

self.navigationItem.setHidesBackButton(true, animated: true)
Matan
  • 685
  • 7
  • 18
2

Here is a version of the answer in

Swift 5

that you can use it from the storyboard:
// MARK: - Hiding Back Button

extension UINavigationItem {

    /// A Boolean value that determines whether the back button is hidden.
    ///
    /// When set to `true`, the back button is hidden when this navigation item
    /// is the top item. This is true regardless of the value in the
    /// `leftItemsSupplementBackButton` property. When set to `false`, the back button
    /// is shown if it is still present. (It can be replaced by values in either
    /// the `leftBarButtonItem` or `leftBarButtonItems` properties.) The default value is `false`.
    @IBInspectable var hideBackButton: Bool {
        get { hidesBackButton }
        set { hidesBackButton = newValue }
    }
}

Every navigation item of a view controller will have this new property in the top section of attributes inspector

Stoyan
  • 1,265
  • 11
  • 20
  • It is wired, can not use self.navigationItem.setHidesBackButton(true, animated: true) or self.navigationItem.hideBackButton = true, only if implement the extension. – knight2016 Jan 21 '21 at 06:36
2

put the below code in viewWillAppear method.

navigationItem.hidesBackButton = true
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Priyank Patel
  • 791
  • 8
  • 6
-6

You may try with the below code

override func viewDidAppear(_ animated: Bool) {
    self.navigationController?.isNavigationBarHidden = true
}
Dilip Jangid
  • 754
  • 1
  • 10
  • 20
-6

enter image description here

Go to attributes inspector and uncheck show Navigation Bar to hide back button.

kit
  • 1,166
  • 5
  • 16
  • 23