Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIMenuController is not visible in iOS 13.2

Tags:

ios

swift

ios13

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)
}
like image 239
Alexander Nikolenko Avatar asked Sep 01 '25 20:09

Alexander Nikolenko


1 Answers

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

window?.makeKeyAndVisible()

or calling it before application:didFinishLaunchingWithOptions: method

like image 171
Artem Trubacheev Avatar answered Sep 04 '25 01:09

Artem Trubacheev