Is it possible to disable the back navigation menu in iOS 14+?

后端 未结 2 499
执笔经年
执笔经年 2020-12-11 05:22

In iOS 14+, tapping and holding on the backBarButtonItem of a UINavigationItem will present the full navigation stack. Then a user may pop to any point in the s

相关标签:
2条回答
  • 2020-12-11 06:04

    It can be done by subclassing UIBarButtonItem. Setting the menu to nil on a UIBarButtonItem doesn't work, but you can override the menu property and prevent setting it in the first place.

    class BackBarButtonItem: UIBarButtonItem {
        @available(iOS 14.0, *)
        override var menu: UIMenu? {
            set {
                // Don't set the menu here
                // super.menu = menu
            }
            get {
                return super.menu
            }
        }
    }
    

    Then you can configure the back button in your view controller the way you like, but using BackBarButtonItem instead of UIBarButtonItem.

    let backButton = BackBarButtonItem(title: "BACK", style: .plain, target: nil, action: nil)
    navigationItem.backBarButtonItem = backButton
    

    This is preferred because you set the backBarButtonItem only once in your view controller's navigation item, and then whatever view controller it will be pushing, the pushed controller will show the back button automatically on the nav bar. If using leftBarButtonItem instead of backBarButtonItem, you will have to set it on every view controller that will be pushed.

    Edit:

    The back navigation menu that appears on long press is a property of UIBarButtonItem. The back button of a view controller can be customized by setting the navigationItem.backBarButtonItem property and by doing so we can control the menu. The only problem with this approach that I see is losing the localization (translation) of the "Back" string which the system button has.

    If you want the disabled menu to be the default behaviour you can implement this in one place, in a UINavigationController subclass conforming to UINavigationControllerDelegate:

    class NavigationController: UINavigationController, UINavigationControllerDelegate {
      init() {
        super.init(rootViewController: ViewController())
        delegate = self
      }
       
      func navigationController(_ navigationController: UINavigationController,
                                willShow viewController: UIViewController, animated: Bool) {
        let backButton = BackBarButtonItem(title: "Back", style: .plain, target: nil, action: nil)
        viewController.navigationItem.backBarButtonItem = backButton
      }
    }
    
    0 讨论(0)
  • 2020-12-11 06:23

    A bit cumbersome but you can set the leftBarButtonItem of your viewController's navigationItem. This removes the back gesture so that's a pain but it does remove the menu.

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