简体   繁体   English

iOS:开始编辑TextField时禁用键盘

[英]iOS: Disable keyboard when begin editing TextField

How can I disable the keyboard when I touch-up inside a UITextField ? 当我在UITextField时,如何禁用键盘?

What I want to do is show a custom digital keyboard instead of the default one. 我想做的是显示一个自定义数字键盘,而不是默认键盘。

If I understand correctly that you are looking to create a custom keyboard in the app, I don't think we need to disable the default keyboard when we touch-up inside a UITextField . 如果我正确理解您要在应用程序中创建自定义键盘,则认为在UITextField时不需要禁用默认键盘。

We just need to create a custom view and assign it to the inputView property of the UITextField to replace the default keyboard. 我们只需要创建一个自定义视图,并将其分配给UITextFieldinputView属性即可替换默认键盘。

For example, something like this: 例如,如下所示:

yourTextField.inputView = yourCustomKeyboardView

See more here . 在这里查看更多。

Note : For Objective-C (Xcode) 注意 :对于Objective-C(Xcode)

In your viewDidLoad: set delegate for textfields which you want to disable. viewDidLoad:为要禁用的文本字段设置委托。

self.textfield.delegate = self;

and insert this delegate function: 并插入此委托函数:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
     if (textField == yourTextfiledOutletInstance) {
         [self showCustomkeyboard];
        return NO;
     }
   return YES;
}

//Show custom keyboard
-(void)showCustomkeyboard{
  // Handle your operation here to show custom keyboard
}

set the UITextField delegate in the ViewController class, and then add this method in the class 在ViewController类中设置UITextField委托,然后在类中添加此方法

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
      [textField resignFirstResponder];
      return NO;
  }

Use resignFirstResponder to dismiss your keyboard. 使用resignFirstResponder关闭键盘。

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
      [textField resignFirstResponder];
      return NO;
}

set inputView on the UITextView to the custom view you want to be used in place of the system keyboard. 设置inputViewUITextView您要安装的系统键盘使用的自定义视图。

myTextView.inputView = myCustomView;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM