如何在点击文本字段时禁用键盘,iOS?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15135784/
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
How to disable keyboard appearing when hitting on a text field , iOS?
提问by Maheta Dhaval K
I have a text field , and i need when the user presses it to show a custom picker.
我有一个文本字段,当用户按下它以显示自定义选择器时我需要。
The picker is shown fine , but the problem is that the keyboard appears on the bottom and i dont want that.
选择器显示正常,但问题是键盘出现在底部,我不想要那个。
This is an iPad project which i am trying to convert from my iphone one. On the iPhone , this works well and the keyboard is always hidden.
这是一个 iPad 项目,我试图从我的 iphone 转换。在 iPhone 上,这很好用,键盘总是隐藏的。
What could i be missing/forgetting to do here ?
我可能会错过/忘记在这里做什么?
EDIT
编辑
For future reference what actually happened here , was that in fact both times (iphone & ipad) the keyboard was nothidden. I just thought that it was hidden in the iphone because my picker , which was popping from the bottom was hiding the keyboard as it was on top of it. But on ipad this wasnt the case.
为了将来参考这里实际发生的事情,实际上两次(iphone和ipad)都没有隐藏键盘。我只是认为它隐藏在 iphone 中,因为我从底部弹出的选择器隐藏了键盘,因为它在它上面。但在 ipad 上,情况并非如此。
Anyway i fixed it , using the delegate method suggested below.
无论如何,我使用下面建议的委托方法修复了它。
Caution , i accepted this answer cause it was the one answering specificallywhat i wanted. The rest of the answers are correct and my considered better for other implementations.
注意,我接受了这个答案的原因这是一个回答具体是什么我想要的。其余的答案是正确的,我认为其他实现更好。
回答by Maheta Dhaval K
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
// Here You can do additional code or task instead of writing with keyboard
return NO;
}
this delegate method will get called first when you hit to textfield and if you write NO as a boolean value means you dont want to begin editing so it will not present Keyboard.
当您点击文本字段时,将首先调用此委托方法,如果您将 NO 写为布尔值,则意味着您不想开始编辑,因此它不会显示键盘。
回答by V.J.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if(textfield == yourtextField)
{
[textfield resignFirstResponder];
// Show you custom picker here....
return NO;
}
}
and you need to implement the uitextfielddelegate
in the controller.
并且您需要uitextfielddelegate
在控制器中实现。
and give assign the delegate to yourtextField
.
并将委托分配给yourtextField
.
回答by AllianceTek
Use textfield delegate.
使用文本字段委托。
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
return NO;
}
回答by Chuck Wolber
It looks like all of these answers take one approach, to simply deny the keyboard before it comes up. This prevents first responder status, which has many advantages.
看起来所有这些答案都采用一种方法,在键盘出现之前简单地拒绝它。这可以防止具有许多优点的第一响应者状态。
One simple approach that allows you to maintain first responder status is to create an empty view and assign that to the inputView property on your input field. If you are using iOS 9 (or later?) you will also have to get rid of the inputAssistantItem objects as well.
一种允许您保持第一响应者状态的简单方法是创建一个空视图并将其分配给输入字段上的 inputView 属性。如果您使用的是 iOS 9(或更高版本?),您还必须删除 inputAssistantItem 对象。
UITextField *field = [[UITextField alloc] init];
field.inputView = self.emptyKeyboard.view;
UITextInputAssistantItem *aItem = [field inputAssistantItem];
aItem.leadingBarButtonGroups = @[];
aItem.trailingBarButtonGroups = @[];
Then if you want to control the field from an alternate view controller, you can do so by adding targets:
然后,如果您想从备用视图控制器控制该字段,您可以通过添加目标来实现:
[field addTarget:self.numberPad action:@selector(editingBegan:) forControlEvents:UIControlEventEditingDidBegin];
[field addTarget:self.numberPad action:@selector(editingEnded:) forControlEvents:UIControlEventEditingDidEnd];
[field addTarget:self.numberPad action:@selector(fieldChanged:) forControlEvents:UIControlEventEditingChanged];
It is also possible to do this a lot more cleanly by subclassing UITextField.
也可以通过继承 UITextField 更干净地做到这一点。
回答by Fangming
swift 3.0 version
迅捷3.0版本
First set the delegate for the text field
首先设置文本字段的委托
self.textfield.delegate = self
Then in an extension
然后在扩展中
extension ViewController: UITextFieldDelegate {
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
return false
}
}
回答by Praveen
Use the textField Delegate,
使用 textField 委托,
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
textField=nil;
return NO;
}
回答by Upendranath Reddy
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
// Here you can do for Specific text Field by
if (textField==(the text field you don't want to show keyboard)) {
NSLog(@"don't show keyboard");
return NO;
}
else {
return YES;
}
}
回答by RAHUL NIMJE
Swift 3/4
斯威夫特 3/4
Add:- UITextFieldDelegatein your class.
Add:- self.textField.delegate = selfIn ViewDidLoad
last one just add this func -
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { return false }
添加:- UITextFieldDelegate在您的班级中。
添加:- self.textField.delegate = self在 ViewDidLoad
最后一个只是添加这个功能 -
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool { return false }