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

查看:447
本文介绍了是否可以在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的新属性,但是尽管按住该按钮时未显示菜单,但它似乎为零.这使我相信这可能是无法更改的特殊行为,但也许我忽略了某些事情.

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无效,但是您可以覆盖menu属性,并避免首先设置它.

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属性来定制视图控制器的后退按钮,这样我们就可以控制菜单.我看到的这种方法的唯一问题是丢失了返回"标记的本地化(翻译).系统按钮具有的字符串.

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天全站免登陆