防止在使用InputView时在UITextField中编辑文本并隐藏光标/插入符号/放大镜 [英] Prevent editing of text in UITextField and hide cursor/caret/magnifying glass while using an inputView

查看:111
本文介绍了防止在使用InputView时在UITextField中编辑文本并隐藏光标/插入符号/放大镜的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何防止在UITextField中编辑文本以及隐藏光标/插入符号/放大镜,但是仍然显示键盘,因为我正在将inputViewUIPickerView/UIDatePickerView一起使用?

How do I prevent the editing of text in a UITextField along with hiding the cursor/caret/magnifying glass, but still present the keyboard, as I am using an inputView with a UIPickerView/UIDatePickerView?

userInteractionEnabled设置为NO无效,因为它不再接收任何触摸,也不会显示键盘.

Setting userInteractionEnabled to NO does not work, because it no longer receives any touches and will not present the keyboard.

推荐答案

子类UITextField

//Disables caret
- (CGRect)caretRectForPosition:(UITextPosition *)position
{
    return CGRectZero;
}

//Disables magnifying glass
-(void)addGestureRecognizer:(UIGestureRecognizer *)gestureRecognizer
{
    if ([gestureRecognizer isKindOfClass:[UILongPressGestureRecognizer class]])
    {
        gestureRecognizer.enabled = NO;
    }
    [super addGestureRecognizer:gestureRecognizer];
}

在您的UITextFieldDelegate中

//Prevent text from being copied and pasted or edited with bluetooth keyboard.
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    return NO;
}

现在,只需根据UIPickerView/UIDatePicker的结果以编程方式设置文本即可.

Now just set your text programmatically from the result of your UIPickerView/UIDatePicker.

这篇关于防止在使用InputView时在UITextField中编辑文本并隐藏光标/插入符号/放大镜的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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