ios 添加按钮以隐藏键盘
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6179534/
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
Add a button to hide keyboard
提问by Vins
On a UITextView to hide the keyboard, there is the method:
在 UITextView 上隐藏键盘,有方法:
...
textfield.returnKeyType = UIReturnKeyDone;
textfield.delegate = self;
....
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
but if I want to leave the button "done" to the "return" and add a button to hide the keyboard, how do I?
但是如果我想将“完成”按钮留在“返回”并添加一个按钮来隐藏键盘,我该怎么做?
回答by Deepak Danduprolu
You can assign a toolbar with a button that dismisses the keyboard as the text field's inputAccessoryView
. A quick example would be,
您可以分配一个带有按钮的工具栏,该按钮将键盘作为文本字段的inputAccessoryView
. 一个简单的例子是,
UIBarButtonItem *barButton = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:textField action:@selector(resignFirstResponder)] autorelease];
UIToolbar *toolbar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
toolbar.items = [NSArray arrayWithObject:barButton];
textField.inputAccessoryView = toolbar;
回答by Jon Vogel
Swift 2.0 version:
斯威夫特 2.0 版本:
//Declared at top of view controller
var accessoryDoneButton: UIBarButtonItem!
let accessoryToolBar = UIToolbar(frame: CGRectMake(0,0,UIScreen.mainScreen().bounds.width, 44))
//Could also be an IBOutlet, I just happened to have it like this
let codeInput = UITextField()
//Configured in viewDidLoad()
self.accessoryDoneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: self, action: #selector(self.donePressed(_:)))
self.accessoryToolBar.items = [self.accessoryDoneButton]
self.codeInput.inputAccessoryView = self.accessoryToolBar
Swift 4:
斯威夫特 4:
//Declared at top of view controller
var accessoryDoneButton: UIBarButtonItem!
let accessoryToolBar = UIToolbar(frame: CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 44))
//Could also be an IBOutlet, I just happened to have it like this
let codeInput = UITextField()
//Configured in viewDidLoad()
self.accessoryDoneButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.done, target: self, action: #selector(self.donePressed))
self.accessoryToolBar.items = [self.accessoryDoneButton]
self.codeInput.inputAccessoryView = self.accessoryToolBar
func donePressed() {
//Causes the view (or one of its embedded text fields) to resign the first responder status.
view.endEditing(true)
}
回答by Julian F. Weinert
This can be done ways easier!
这可以更容易地完成!
I made a custom view in IB, in my viewController.h I just made an IBOutlet UIView *accessoryView;
, connected them and an - (IBAction)dismissKeyboard;
我在 IB 中创建了一个自定义视图,在我的 viewController.h 中我刚刚创建了一个IBOutlet UIView *accessoryView;
,将它们和一个- (IBAction)dismissKeyboard;
I put in the view a toolbar with a done button, made a connection to the IBAction an wrote:
[textView resignFirstResponder]
and
我把视图中的工具栏与完成按钮,做出了IBAction为一个写了一个连接:
[textView resignFirstResponder]
和
- (void)viewDidLoad
{
textView.inputAccessoryView = accessoryView;
[super viewDidLoad];
}
But actually that looks a bit strange and non-apple-style… Got an idea?
但实际上这看起来有点奇怪和非苹果风格......有想法吗?