隐藏UITextField的游标 [英] Hide the cursor of an UITextField
问题描述
我使用 UITextField
与 UIPickerView
为 inputView
,所以当用户点击文本字段时,会调用选择器来从中选择一个选项。
几乎一切正常,但我有一个问题:当文本字段处于活动状态时,光标仍然在文本字段中闪烁,这是丑陋和不适当的,因为不希望用户键入到字段中,并且不会显示键盘。我知道我可以通过在文本字段上设置编辑
到 NO
和跟踪触摸来解决这个问题,或者将其替换为自定义样式的按钮,并通过代码调用选择器。但是,我想使用 UITextFieldDelegate
方法对文本字段上的所有事件处理,并使用按钮替换文本字段不允许这种方法。 p>
如何简单地隐藏 UITextField
上的光标?
由OP提供的答案,从问题正文复制,以帮助清理日益增长的未回答问题的尾巴。
我发现了另一个解决方案:subclass UIButton
并覆盖这些方法
- (UIView *)inputView {
return inputView_;
}
- (void)setInputView:(UIView *)anInputView {
if(inputView_!= anInputView){
[inputView_ release];
inputView_ = [anInputView retain];
}
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
现在按钮作为 UIResponder
具有类似于 UITextField
的行为和一个非常简单的实现。
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.
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.
How can I simply hide the cursor on the UITextField
instead?
Answer provided by the OP, copied from the question body to help clean up the ever growing tail of unanswered questions.
I found another solution: subclass UIButton
and override these methods
- (UIView *)inputView {
return inputView_;
}
- (void)setInputView:(UIView *)anInputView {
if (inputView_ != anInputView) {
[inputView_ release];
inputView_ = [anInputView retain];
}
}
- (BOOL)canBecomeFirstResponder {
return YES;
}
Now the button, as a UIResponder
, have a similar behavior than UITextField
and an implementation pretty straightforward.
这篇关于隐藏UITextField的游标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!