简体   繁体   中英

UIMenuController is not visible in iOS 13.2

I have long press handler that shows UIMenuController , it works as usual on < ios13.2, for example on 13.1 it works fine, but on ios13.2 it's not shown, here's methods that I had:

private func longPressHandler(sender: UILongPressGestureRecognizer) {
    guard
        sender.state == .began,
        let senderView = sender.view,
        let superView = sender.view?.superview
    else {
        return
    }

    senderView.becomeFirstResponder()

    UIMenuController.shared.setTargetRect(senderView.frame, in: superView)
    UIMenuController.shared.setMenuVisible(true, animated: true)
}

private func makeMenuController() {
    UIMenuController.shared.menuItems = [
        UIMenuItem(title: "ui.report".localized, action: ChatCustomMenuItems.report),
        UIMenuItem(title: "ui.chat.reply".localized, action: ChatCustomMenuItems.reply),
        UIMenuItem(title: "ui.action.block".localized, action: ChatCustomMenuItems.block)
    ]
}

In the documentation I've found out that setTargetRect and setMenuVisible are deprecated 在此处输入图像描述

Changing like this, still doesn't help. Any solution?

if #available(iOS 13.0, *) {
    UIMenuController.shared.isMenuVisible = true
    UIMenuController.shared.showMenu(from: superView, rect: senderView.frame)
} else {
    UIMenuController.shared.setTargetRect(senderView.frame, in: superView)
    UIMenuController.shared.setMenuVisible(true, animated: true)
}

I've had exactly the same problem! The problem can be caused by not calling

window?.makeKeyAndVisible()

or calling it before application:didFinishLaunchingWithOptions: method

In my case, I need to call becomeFirstResponder to make the menu showing:

window?.makeKey()
becomeFirstResponder()

只要确保你没有在你的代码中创建 UIMenuController 的其他实例,就像这样let menu = UIMenuController()

Before that you should override the property like this:

override var canBecomeFirstResponder: Bool{
    get{
        return true
    }
}

Then it will work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM