iPhone - 可能不显示键盘但仍然在UITextField中显示光标?

Swa*_*tik 5 iphone uitextfield

当用户点击UITextField时,我想要显示一个自定义键盘.但同时我想在文本字段中显示光标.如果if为canBecomeFirstResponder返回NO,则它不显示默认键盘,但也不显示光标.

有人可以帮帮我吗?

谢谢.

art*_*ovm 5

问题的答案是为自定义键盘创建一个视图,然后编辑UITextFieldinputView属性并将其传递给自定义键盘的视图.

我希望有所帮助.


Yog*_*ogi 5

覆盖 UITextFieldDelegate 中的以下两个方法。请注意,此方法对 UITextField 和 UITextView 都有效(在这种情况下,您可以覆盖 UITextViewDelegate 中的相应方法)

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    if (!textField.inputView) {
        //hides the keyboard, but still shows the cursor to allow user to view entire text, even if it exceeds the bounds of the textfield
        textField.inputView = [[UIView alloc] initWithFrame:CGRectZero];
    }
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    return NO;
}
Run Code Online (Sandbox Code Playgroud)


And*_*iih 0

我不确定这一点,但为什么不只使用与文本字段内容相同的 UILabel 并装饰成看起来像带有光标的文本字段。当您需要输入时,将其替换为 UITextField。