Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - How to hide back button in navigation item?

Tags:

ios

swift

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];
like image 686
Nurdin Avatar asked Dec 09 '14 07:12

Nurdin


People also ask

How do I hide the back button on my navigation?

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 do I hide the back button for navigation view in SwiftUI?

The . navigationBarBackButtonHidden(true) will hide the back button.

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.


9 Answers

According to the documentation for UINavigationItem :

self.navigationItem.setHidesBackButton(true, animated: true)
like image 89
Paulw11 Avatar answered Oct 21 '22 01:10

Paulw11


In case you're using a UITabBarController:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.tabBarController?.navigationItem.hidesBackButton = true
}
like image 40
Bruno Cunha Avatar answered Oct 21 '22 02:10

Bruno Cunha


Swift

// remove left buttons (in case you added some)
 self.navigationItem.leftBarButtonItems = []
// hide the default back buttons
 self.navigationItem.hidesBackButton = true
like image 37
Marwen Doukh Avatar answered Oct 21 '22 01:10

Marwen Doukh


This is also found in the UINavigationController class documentation:

navigationItem.hidesBackButton = true
like image 42
Amiru Homushi Avatar answered Oct 21 '22 02:10

Amiru Homushi


Put it in the viewDidLoad method

navigationItem.hidesBackButton = true 
like image 21
Harjeet Singh Avatar answered Oct 21 '22 01:10

Harjeet Singh


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

self.navigationItem.setHidesBackButton(true, animated: true)
like image 41
Matan Avatar answered Oct 21 '22 00:10

Matan


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

like image 24
Stoyan Avatar answered Oct 21 '22 01:10

Stoyan


self.navigationItem.setHidesBackButton(true, animated: false)
like image 40
Vinay Podili Avatar answered Oct 21 '22 01:10

Vinay Podili


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

like image 25
Priyank Patel Avatar answered Oct 21 '22 02:10

Priyank Patel