Search code examples
swiftxcodeuitextfield

How to remove the option window for selected text in textfield, Swift


When text is selected/marked in my app this window pops up with different options if the user would want to copy/cut out etc, but I want this window not to be able to pop up at all. Anyone knows how to achieve this?

Thanks in advance, Pontusenter image description here


Solution

  • You must create a subclass of your UITexField or UITextView (or UILabel) depending on which one you want to disable the UIMenu.
    Then override:

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

    And return no for the selectors you want to disable.
    If you prefer to disable selection of text in a UITextView, you can disable the selection behavior.