xcode iPhone:是否可以在点击 UITextField 时隐藏键盘?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8633448/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 22:50:02  来源:igfitidea点击:

iPhone:Is it possible to hide keyboard while tapping UITextField?

iosiphoneobjective-cxcodeuitextfield

提问by iPhone

In my application I have to fill UITextFieldfrom the value exist in UIPickerViewnot through keyboard.

在我的应用程序中,我必须不通过键盘来填充UITextField存在的值UIPickerView

I have 2 UITextFields. In 1st textfield value is fetched from keyboard and in 2nd textfield value is fetched from UIPickerView

我有 2 UITextFields。从键盘获取第一个文本字段值,从第二个文本字段获取值UIPickerView

so on tapping 2nd textfield I want to hide keyboard and show UIPickerView

所以点击第二个文本框我想隐藏键盘并显示 UIPickerView

here I add what I do for your better reference

在这里我添加我所做的以供您更好地参考

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
      [pickerToolBar setHidden:NO];//shows toolbar
        [pickerTime setHidden:NO];//shows UIPickerView
        [textField resignFirstResponder];
}

but after writing the above code the keyboard does not disappear.

但是写完上面的代码后,键盘并没有消失。

if you have any idea then plz help me...

如果您有任何想法,请帮助我...

采纳答案by Kinetic Stack

If you are populating the UITextField based on the UIPicker, why not simply disable user interaction with the UITextField? You can still put data there and the user will not be able to change it. To force the keyboard to stay hidden however, you can make the UITextField detect when it is touched/changed/etc and then have it resign first responder.

如果您基于 UIPicker 填充 UITextField,为什么不简单地禁用用户与 UITextField 的交互?您仍然可以将数据放在那里,用户将无法更改它。但是,要强制键盘保持隐藏状态,您可以让 UITextField 检测它何时被触摸/更改/等,然后让它退出第一响应者。

回答by mashios

Try UITextfielddelegate methods. put the following code in texfield didBeginEditingmethod.

尝试UITextfield委托方法。将以下代码放入 texfielddidBeginEditing方法中。

[texfield resignfirstresonder];

Then the text field will block editing and keyboard will not appear. Make sure you attached the text field delegate to the file owner.

然后文本字段将阻止编辑并且不会出现键盘。确保将文本字段委托附加到文件所有者。

回答by Ravi

 UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyboard)];
 [self.view addGestureRecognizer:gestureRecognizer];
 gestureRecognizer.cancelsTouchesInView = NO;


- (void) hideKeyboard 
{
     [textfieldname1 resignFirstResponder];
     [textfieldname2 resignFirstResponder];
}

回答by Garfbargle

These answers might work but no one gave you the best answer; You can replace the keyboard view with the uipickerview when the textfield is tapped :)

这些答案可能有用,但没有人给你最好的答案;当文本字段被点击时,您可以用 uipickerview 替换键盘视图:)

UITextField *field = [[UITextField alloc] init];
field.inputView = [[UIPickerView alloc] init];

回答by Kanan Vora

It is not possible to hide the keyboard while the focus is inside the UITextField...

当焦点位于 UITextField 内时,无法隐藏键盘...

You must put UIPicker at a level where it does not get hidden behind the keyboard, so that it does not become an obstacle for you while selecting the value..

您必须将 UIPicker 放在不会隐藏在键盘后面的级别,以便在选择值时不会成为您的障碍。

And if you're selecting value from UIPicker then why you need to tap in the UITextField, so if you're not tapping in the UiTextField, the keyboard will not be shown...

如果你从 UIPicker 中选择值,那么为什么你需要点击 UITextField,所以如果你没有点击 UiTextField,键盘将不会显示......

Cheers!!!

干杯!!!

回答by Hiren

try with text field editable property as FALSE or user interaction enable property false and add one custom button with same size of your textfield with the button event of opening picker view.

尝试使用文本字段可编辑属性为 FALSE 或用户交互启用属性 false 并添加一个与文本字段大小相同的自定义按钮,并使用打开选择器视图的按钮事件。