添加隐藏键盘的按钮
在 UITextView 上隐藏键盘有一个方法:
...
textfield.returnKeyType = UIReturnKeyDone;
textfield.delegate = self;
....
-(BOOL)textFieldShouldReturn:(UITextField *)textField {
[textField resignFirstResponder];
return YES;
}
但是如果我想将“完成”按钮保留到“返回”并添加一个按钮来隐藏键盘,我该怎么办?
On a UITextView to hide the keyboard, there is the method:
...
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以分配一个带有按钮的工具栏,该按钮可将键盘关闭为文本字段的
inputAccessoryView
。一个简单的例子是,You can assign a toolbar with a button that dismisses the keyboard as the text field's
inputAccessoryView
. A quick example would be,Swift 2.0 版本:
Swift 4:
UIToolBar 文档
'inputAccessoryView'文档
Swift 2.0 version:
Swift 4:
UIToolBar Documentation
'inputAccessoryView' documentation
这可以更容易地完成!
我在 IB 中创建了一个自定义视图,在 viewController.h 中,我刚刚创建了一个
IBOutlet UIView *accessoryView;
,将它们连接起来并添加 了一个
- (IBAction)dismissKeyboard;
查看带有完成按钮的工具栏,与 IBAction 建立连接并写道:[textView resignFirstResponder]
但实际上这看起来有点奇怪并且非苹果风格......有什么想法吗?
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;
I put in the view a toolbar with a done button, made a connection to the IBAction an wrote:
[textView resignFirstResponder]
andBut actually that looks a bit strange and non-apple-style… Got an idea?