2

I want remove UIMenuController item.

This textfield is not a UITextField.

That textfield is UIWebView's textfield. Not owned native.

And I tried this.

override func viewDidLoad() {
    super.viewDidLoad()
    ...
    UIMenuController.shared.isMenuVisible = false
}

override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        return false
    }

But result is always same. How to remove UIMenuItem??

I want only "copy" button. How can I do that??

enter image description here

2 Answers2

3

To remove UIMenuController item in swift4:

class DNGWebView: WKWebView {
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
            return false
    }
}

As Peter Stuart said: Subclass the view that's presenting the menu (eg. UITextView)

then override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool

return false for the menu items you don't want to appear.

To illustrate it intuitively:

import WebKit

class DNGWebView: WKWebView {
    override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
        if action == #selector(copy(_:)){
            return true
        }
        else{
            return false
        }
    }
}

11

dengApro
  • 3,848
  • 2
  • 27
  • 41
  • I'm using in UIWebView. Not a WKWebView. If it does not matter, I do not work like you. All the menus are always open ... –  Jul 16 '18 at 08:57
  • @allanWay, exactly what you said. WKWebView is OK. UIWebView is as usual. – dengApro Jul 16 '18 at 09:04
1

You can create custom menu items with action and assign to you UIMenuViewController by following code:

let menuCustom1 = UIMenuItem(title: "Custom 1", action: #selector(<MethodName>))
let menuCustom2 = UIMenuItem(title: "Custom 2", action: #selector(<MethodName>))
let menuCustom3 = UIMenuItem(title: "Custom 3", action: #selector(<MethodName>))

UIMenuController.shared.isMenuVisible = true
UIMenuController.shared.menuItems = [menuCustom1, menuCustom2, menuCustom3]
UIMenuController.shared.update()

Output:

UIMenuController custom items

I hope this will help you.

Sagar Chauhan
  • 5,715
  • 2
  • 22
  • 56
  • I have already tried that method. The method only adds items. It does not change or remove the original item. –  Jul 16 '18 at 06:29
  • same result...See that https://imgur.com/a/rvw3Cip ////// https://imgur.com/a/CV10pT1 –  Jul 16 '18 at 06:49