在UITextField的editingDidBegin中显示UIMenuController [英] Display UIMenuController in editingDidBegin of a UITextField

查看:219
本文介绍了在UITextField的editingDidBegin中显示UIMenuController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只想在文本字段激活后立即显示UIMenuController。

我现在做的是:

I simply want to display the UIMenuController right after a textfield has become active.
What i'm doing right now is:

- (IBAction)textFieldeditingDidBegin:(UITextField *)sender {
    // textfield menu item
    UIMenuController *menu = [UIMenuController sharedMenuController];
    [menu setTargetRect:sender.frame inView:self.view];
    [menu setMenuVisible:YES animated:YES];
}

方法被调用,但它不会显示菜单... < br>
如果我在文本字段上触摸+保持手势,它会定期出现。

The method gets called but it just will not display the menu...
If i do a touch+hold gesture on the textfield it comes up regularly.

我希望有一个简单的解决方案,
感谢

I hope there's a simple solution for that, Thanks

推荐答案

我找到了一个很好的解决方案。

I found a good solution to your question.

使用此方法开始编辑文本字段时,可以轻松地使 UIMenuController 出现:

You can easily make the UIMenuController appear when you start editing your text field with this method:

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    double delayInSeconds = 0.1;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        UIMenuController *menu = [UIMenuController sharedMenuController];
        [menu setTargetRect:textField.frame inView:textField.superview];
        [menu setMenuItems:[NSArray arrayWithObjects:
                            [[UIMenuItem alloc] initWithTitle:@"Test" action:@selector(test)],
                            nil]];
        [menu setMenuVisible:YES animated:YES];
    });
}

我使用 dispatch_after 调用以确保之后> c> 所有默认系统调用都显示。

I use the dispatch_after call to make sure that the menu is show after all the defaults system calls on the UITextField are completed.

我也改变了 setTargetRect :: 方法的 inView:self.view inView:textField.superview 以确保菜单在文本字段的容器视图中正确显示。

I also changed the inView:self.view part of the setTargetRect:: method with inView:textField.superview to be sure the menu is showing correctly in the container view of the text field.

如果您还要禁用 UITextField 的默认菜单控件,您可以将此方法添加到您的控制器:

If you also want to disable the default menu controls for a UITextField you can add this method to your controller:

- (BOOL)canPerformAction:(SEL)action withSender:(id)sender
{
    if (action == @selector(cut:))
        return NO;
    else if (action == @selector(copy:))
        return NO;
    else if (action == @selector(paste:))
        return NO;
    else if (action == @selector(select:) || action == @selector(selectAll:))
        return NO;
    else
        return [super canPerformAction:action withSender:sender];
}

这项工作真的在模拟器。我希望这将帮助你!

This work really well in the simulator. I hope this will help you!

这篇关于在UITextField的editingDidBegin中显示UIMenuController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆