UITextField how to disable the paste?
Asked
Active
Viewed 1.7k times
12
-
This answer was given by PengOne [PengOne](http://stackoverflow.com/users/544050/pengone "PengOne") in this question: http://stackoverflow.com/questions/6701019/how-to-disable-copy-paste-option-from-uitextfield-programmatically?rq=1 – Jeff Wolski Apr 01 '13 at 15:05
-
oh,I'm sorry,I will not repeat the problem later. promise. – isaced May 25 '15 at 06:52
1 Answers
77
overrides the canPerformAction:withSender:
method to return NO
for actions that you don't want to allow:
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(paste:))
return NO;
if (action == @selector(select:))
return NO;
if (action == @selector(selectAll:))
return NO;
return [super canPerformAction:action withSender:sender];
}
In Above Code you need to write only for paste
Another way
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender {
UIMenuController *menuController = [UIMenuController sharedMenuController];
if (menuController) {
[UIMenuController sharedMenuController].menuVisible = NO;
}
return NO;
}
Also check This link
EDITED
In iOS 7, you can do such like,,
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO];
}];
return [super canPerformAction:action withSender:sender];
}
For Swift User
override public func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if action == #selector(copy(_:)) || action == #selector(paste(_:)) {
return false
}
return true
}
If you want to Open Date Picker or Picker view on TEXTFIELD click then below code work.
Add below two methods in your class.
//Hide Menu View
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
if YOURTEXTFIELD.isFirstResponder {
DispatchQueue.main.async(execute: {
(sender as? UIMenuController)?.setMenuVisible(false, animated: false)
})
return false
}
return super.canPerformAction(action, withSender: sender)
}
//MUST Implement
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return false
}

Renish Dadhaniya
- 10,642
- 2
- 31
- 56

iPatel
- 46,010
- 16
- 115
- 137
-
3
-
1
-
4
-
2
-
-
@Balu - In my app used same above code it's working fine in iOS 9.3 too. – iPatel Jun 22 '16 at 13:05
-
2@iPatel - i tried in simulator. In Device its working. Thankq. – S P Balu Kommuri Jun 23 '16 at 10:12