我正在实现自定义UIMenuController,并试图找出。我如何合法地禁用“复制”和“定义”UIMenuItems of UIMenuController in UITextfield?Textfield不可编辑。我试图禁用“复制”,使用:
-(BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(copy:))
{
return NO;
}
return [super canPerformAction:action withSender:sender];
}
- (IBAction)tapTextViewGesture:(id)sender {
UIMenuItem *myItem1 = [[UIMenuItem alloc] initWithTitle:@"myItem1" action:@selector(myItem1Pressed:)];
UIMenuItem *myItem2 = [[UIMenuItem alloc] initWithTitle:@"myItem2" action:@selector(myItem2Pressed:)];
UIMenuItem *myItem3 = [[UIMenuItem alloc] initWithTitle:@"myItem3" action:@selector(myItem3Pressed:)];
// Access the application's shared menu
UIMenuController *menu = [UIMenuController sharedMenuController];
[menu setMenuItems:[NSArray arrayWithObjects:myItem1,myItem2,myItem3, nil]];
CGRect menuRect = CGRectMake(20, 50, 200, 0);
// Show the menu from the cursor's position
[menu setTargetRect:menuRect inView:self.view];
[menu setMenuVisible:YES animated:YES];
}但是菜单仍然显示“复制”和“定义”UIMenuItems。我怎样才能禁用他们,只留下我的物品?
发布于 2015-01-06 21:02:58
最后,通过子类UITextView (为它创建自定义类)并添加
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
if (action == @selector(copy:))
{
return NO;
}
return NO;
}在我的自定义.m子类的TextView文件中。
在那之后,“拷贝”不再出现,无论有没有[menu update];
发布于 2017-03-09 13:10:26
在viewController.m中实现此实例方法:
**- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
if ([_targetTextField isFirstResponder]) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[[UIMenuController sharedMenuController] setMenuVisible:NO animated:NO];
}];
}
return [super canPerformAction:action withSender:sender];
}**此方法检查目标文本字段是否是第一个响应程序。如果是的话,NSOperationQueue会为sharedMenuController操作创建一个单独的线程,将其可见性和动画设置为no,使其不能用于复制、粘贴等。返回语句调用UIResponder的canPerformAction方法来通知实现它的方法。
发布于 2018-11-01 15:03:56
SWIFT4.2. && Xcode 10,适用于我:
public extension UITextView {
override func canPerformAction(_ action: Selector, withSender sender: Any?) -> Bool {
// Requested action to prevent:
guard action != #selector(copy(_:)) else { return false } // disabling copy
// Further actions to prevent:
// guard action != #selector(cut(_:)) else { return false } // disabling cut
// guard action.description != "_share:" else { return false } // disabling share
return super.canPerformAction(action, withSender: sender)
}
}为了完成这项工作,您必须创建UITextField/UITextView的子类,并确保调用super.canPerformAction(_:withSender:)上的super!
https://stackoverflow.com/questions/27747140
复制相似问题