Search code examples
iosswiftios13

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 enter image description here

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

Solution

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

    window?.makeKeyAndVisible()
    

    or calling it before application:didFinishLaunchingWithOptions: method