Swift - How to hide back button in navigation item?

后端 未结 9 900
走了就别回头了
走了就别回头了 2020-11-30 23:06

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 foun

相关标签:
9条回答
  • 2020-11-30 23:53

    You may try with the below code

    override func viewDidAppear(_ animated: Bool) {
        self.navigationController?.isNavigationBarHidden = true
    }
    
    0 讨论(0)
  • 2020-12-01 00:00

    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

    0 讨论(0)
  • 2020-12-01 00:02

    enter image description here

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

    0 讨论(0)
提交回复
热议问题