Nurdin
Nurdin

Reputation: 23901

Swift - How to hide back button in navigation item?

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

Upvotes: 142

Views: 164159

Answers (13)

Priyank Patel
Priyank Patel

Reputation: 880

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

Upvotes: 5

Priyank Patel
Priyank Patel

Reputation: 880

put the below code in viewWillAppear method.

navigationItem.hidesBackButton = true

Upvotes: 3

Alwaleed A. Hamam
Alwaleed A. Hamam

Reputation: 155

In SwiftUI

.navigationBarBackButtonHidden(true)

Upvotes: 7

Vinay Podili
Vinay Podili

Reputation: 345

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

Upvotes: 4

Paulw11
Paulw11

Reputation: 115083

According to the documentation for UINavigationItem :

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

Upvotes: 471

Bruno Cunha
Bruno Cunha

Reputation: 1810

In case you're using a UITabBarController:

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

Upvotes: 54

Harjeet Singh
Harjeet Singh

Reputation: 111

Put it in the viewDidLoad method

navigationItem.hidesBackButton = true 

Upvotes: 10

Matan
Matan

Reputation: 1003

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

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

Upvotes: 4

Stoyan
Stoyan

Reputation: 1315

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

Upvotes: 2

neeraj sachdeva
neeraj sachdeva

Reputation: 15

enter image description here

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

Upvotes: -5

Marwen Doukh
Marwen Doukh

Reputation: 2050

Swift

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

Upvotes: 28

Amiru Homushi
Amiru Homushi

Reputation: 139

This is also found in the UINavigationController class documentation:

navigationItem.hidesBackButton = true

Upvotes: 10

Dilip Jangid
Dilip Jangid

Reputation: 774

You may try with the below code

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

Upvotes: -5

Related Questions