是否可以在 iOS 14+ 中禁用后退导航菜单? [英] Is it possible to disable the back navigation menu in iOS 14+?

查看:39
本文介绍了是否可以在 iOS 14+ 中禁用后退导航菜单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 iOS 14+ 中,点击并按住 UINavigationItem 的 backBarButtonItem 将呈现完整的导航堆栈.然后用户可以弹出到堆栈中的任何点,而以前用户所能做的只是点击该项目以弹出堆栈中的一项.

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 stack, whereas previously all a user could do was tap this item to pop one item in the stack.

是否可以禁用此功能?UIBarButtonItem 有一个名为 menu 的新属性,但尽管在按住按钮时显示菜单,但它似乎为 nil.这让我相信这可能是无法改变的特殊行为,但也许我忽略了一些东西.

Is it possible to disable this? UIBarButtonItem has a new property named menu, but it appears to be nil in spite of showing a menu when holding on the button. This leads me to believe this may be special behavior that cannot be changed, but perhaps I'm overlooking something.

推荐答案

可以通过继承 UIBarButtonItem 来完成.在 UIBarButtonItem 上将菜单设置为 nil 不起作用,但您可以覆盖菜单属性并防止首先对其进行设置.

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
        }
    }
}

然后您可以按照自己喜欢的方式在视图控制器中配置后退按钮,但使用 BackBarButtonItem 而不是 UIBarButtonItem.

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

这是首选,因为您只在视图控制器的导航项中设置了一次 backBarButtonItem,然后无论将要推送的视图控制器,推送的控制器都会在导航栏上自动显示后退按钮.如果使用 leftBarButtonItem 而不是 backBarButtonItem,则必须在要推送的每个视图控制器上设置它.

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.

长按时出现的后退导航菜单是 UIBarButtonItem 的一个属性.可以通过设置 navigationItem.backBarButtonItem 属性来自定义视图控制器的后退按钮,这样我们就可以控制菜单.我看到的这种方法的唯一问题是丢失了Back"的本地化(翻译).系统按钮的字符串.

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.

如果您希望禁用菜单成为默认行为,您可以在一个符合 UINavigationControllerDelegate 的 UINavigationController 子类中实现这一点:

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
  }
}

这篇关于是否可以在 iOS 14+ 中禁用后退导航菜单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆