Swift - How to hide back button in navigation item?

IosSwift

Ios Problem Overview


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];

Ios Solutions


Solution 1 - Ios

According to the documentation for UINavigationItem :

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

Solution 2 - Ios

In case you're using a UITabBarController:

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

Solution 3 - Ios

Swift

// remove left buttons (in case you added some)
 self.navigationItem.leftBarButtonItems = []
// hide the default back buttons
 self.navigationItem.hidesBackButton = true

Solution 4 - Ios

This is also found in the UINavigationController class documentation:

navigationItem.hidesBackButton = true

Solution 5 - Ios

Put it in the viewDidLoad method

navigationItem.hidesBackButton = true 

Solution 6 - Ios

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

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

Solution 7 - Ios

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

Solution 8 - Ios

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

Solution 9 - Ios

In SwiftUI .navigationBarBackButtonHidden(true)

Solution 10 - Ios

You may try with the below code

override func viewDidAppear(_ animated: Bool) {
    self.navigationController?.isNavigationBarHidden = true
}

Solution 11 - Ios

enter image description here

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNurdinView Question on Stackoverflow
Solution 1 - IosPaulw11View Answer on Stackoverflow
Solution 2 - IosBruno CunhaView Answer on Stackoverflow
Solution 3 - IosMarwen DoukhView Answer on Stackoverflow
Solution 4 - IosAmiru HomushiView Answer on Stackoverflow
Solution 5 - IosHarjeet SinghView Answer on Stackoverflow
Solution 6 - IosMatanView Answer on Stackoverflow
Solution 7 - IosStoyanView Answer on Stackoverflow
Solution 8 - IosVinay PodiliView Answer on Stackoverflow
Solution 9 - IosAlwaleed A. HamamView Answer on Stackoverflow
Solution 10 - IosDilip JangidView Answer on Stackoverflow
Solution 11 - Iosneeraj sachdevaView Answer on Stackoverflow