隐藏 UITextField 的光标 [英] Hide the cursor of a UITextField

查看:52
本文介绍了隐藏 UITextField 的光标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 UIPickerViewUITextField 作为其 inputView,这样当用户点击文本字段时,就会调用一个选择器让他们从中选择一个选项.

I am using a UITextField with a UIPickerView for its inputView, so that when the user taps the text field, a picker is summoned for them to select an option from.

几乎一切正常,但我有一个问题:当文本字段处于活动状态时,光标仍然在文本字段中闪烁,这很丑陋且不合适,因为不希望用户在该字段中输入并且没有显示键盘.我知道我可以通过在文本字段上将 editing 设置为 NO 并跟踪它的触摸来解决这个问题,或者用自定义样式的按钮替换它,然后调用通过代码选择器.但是,我想对文本字段上的所有事件处理使用 UITextFieldDelegate 方法,并且诸如用按钮替换文本字段之类的技巧不允许这种方法.

Nearly everything works, but I have one problem: the cursor still flashes in the text field when it is active, which is ugly and inappropriate, since the user is not expected to type into the field and is not presented with a keyboard. I know I could hackily solve this by setting editing to NO on the text field and tracking touches on it, or by replacing it with a custom-styled button, and summoning the picker via code. However, I want to use the UITextFieldDelegate methods for all the event handling on the text field and hacks such as replacing the text field with a button do not permit this approach.

如何简单地将光标隐藏在 UITextField 上?

How can I simply hide the cursor on the UITextField instead?

推荐答案

只需继承 UITextField 并覆盖 caretRectForPosition

Simply subclass UITextField and override caretRectForPosition

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

这篇关于隐藏 UITextField 的光标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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