2016-06-21 71 views
4

我有要求在用戶選擇任何文本時在uiwebview上顯示菜單項。 enter image description here如何創建自定義UIMenuController,其中只包含非默認的自定義項目?

我已經試過

let highlightMenuItem = UIMenuItem(title: "Highlight", action: #selector(ViewController.hightlight)) 

UIMenuController.sharedMenuController().menuItems = [highlightMenuItem] 

但這隻能使用默認現有的追加更多的菜單項。因爲這

enter image description here enter image description here

有沒有出路,只有菜單項來實現這一複製,突出顯示並注意?

回答

7

您可以通過繼承UIWebView和覆蓋canPerformAction(Swift 3)來實現此目的。然後,您需要做的就是返回false,無論您想要禁用哪一項操作。

例子:

class EditedUIMenuWebView: UIWebView { 

    override func canPerformAction(_ action: Selector, withSender sender: AnyObject?) -> Bool { 
    if action == #selector(cut(_:)) { 
     return false 
    } 
    if action == #selector(paste(_:)) { 
     return false 
    } 
    if action == #selector(select(_:)) { 
     return false 
    } 
    if action == #selector(selectAll(_:)) { 
     return false 
    } 
    ... 

    return super.canPerformAction(action, withSender: sender) 
    } 

} 

如果您有任何疑問,請諮詢!

編輯如果要禁用所有行動,但一些可能更容易到剛剛在canPerformAction回到false和你想,像這樣的人返回true

override func canPerformAction(_ action: Selector, withSender sender: AnyObject?) -> Bool { 
    if action == #selector(copy(_:)) || action == #selector(customMethod(_:)) { 
    return true 
    } 
    ... 
    return false 
} 
+0

@Nitesh看到我的編輯。 – Ike10

+0

謝謝@Ike10,進行編輯,是的,這是忽略其他操作的好方法。只保留我們的習俗。 – Nitesh

+0

如果你想過濾出複製動作,這不起作用。爲了過濾複製操作,您需要顯示自己的菜單控制器 –