禁用长按后退按钮(标注菜单) [英] Disable Long Press back Button (callout menu)

查看:274
本文介绍了禁用长按后退按钮(标注菜单)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

iOS14引入了长按后退按钮的功能,该按钮可打开一个标注菜单,以返回到堆栈中的特定VC.

iOS14 introduced that long-press on back button which opens a callout menu to go back to specific VC in stack.

我想禁用它,是否有可能做这样的事情,如果可以,怎么办?

I would like to disable it, is there a possibility to do such a thing, and if yes how ?

谢谢

推荐答案

可以通过对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 the preferred way 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.

这篇关于禁用长按后退按钮(标注菜单)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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