0

I wanted to use a custom UIMenuController in WKWebView.

First, I wanted to get rid of the default menu (Copy, Look up, Share), but for some reason I don't know, but it hasn't disappeared.

override open func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        switch action {
        case #selector(highlightHandler):
            return true
        default:
            return false
        }
    }
func enableCustomMenu() {
        let memo = UIMenuItem(title: "메모", action: #selector(highlightHandler))
        UIMenuController.shared.menuItems = [memo]
        UIMenuController.shared.update()
    }
 @objc func highlightHandler(sender: UIMenuItem) { }

I tried using the code above to remove the default menuItems and add custom menuItems called "메모", but it didn't.

enter image description here

How can I show only the items I want called "메모"?

김민식
  • 97
  • 5

1 Answers1

0

canPerformAction() cannot reject an option in most cases. It can only tell the system that the class it's being called in is willing to provide the needed function. Returning False just says "I can't do that one", and then the next item in the responder chain is called and eventually something is found that says "Yes, I can do that". Having said that, it seems that I get a different result if I override this function on the item that is the first responder. In that case, False actually seems to disable the command. So if you can implement canPerformAction() on the first-responder, do that. If not...

Basically you have to temporarily break the responder chain. You do that by overriding the UIResponder "next" variable so that it conditionally returns nil when you want the chain broken. You don't want it to leave it broken for long or bad things will happen. Anything that was approved by the FirstResponder or things in the responder chain between First and you will still be approved, but that will stop approval of things after you in the chain.

BarryD
  • 41
  • 6