Disable Keyboard for UITextfield

前提是你 提交于 2019-12-22 10:28:36

问题


I'm wondering how to disable the inputview of a UITextfield. Setting textField.inputView = nil; or [textField setInputView:nil] in ShouldBeginEditing doesn't do anything, and using the userInteraction property removes the ability to interact with the field. Ideally, I'd like to remove both the cursor and the keyboard while still being able interact with and switch between textfield methods, using ShouldBeginEditing and ShouldEndEditing. Is there any way to accomplish this?


回答1:


You should do this:

myTextField.inputView = UIView.new; //Empty UIView

Setting it to nil just means the default keyboard is used.

To get rid of the caret, subclass the UITextField and override caretRectForPosition:

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



回答2:


Try this :

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
  return NO;  // Hide both keyboard and blinking cursor.
}

or

-(void)textFieldDidBeginEditing:(UITextField *)textField{
  [textField resignFirstResponder]; // hides keyboard
}


来源:https://stackoverflow.com/questions/18214969/disable-keyboard-for-uitextfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!