[英]Hide Virtual Keyboard of UITextView when 'Done' Presses
I want to hide ( resignFirstResponder
) the virtual keyboard of UITextView
when 'Done' presses. 当'完成'按下时,我想隐藏(
resignFirstResponder
) UITextView
的虚拟键盘。 Theres no 'Did End on Exit' in UITextView
. 在
UITextView
没有'退出时结束'。 In UITextField
i connect the 'Did End on Exit' with an IBAction
and call resignFirstResponder
method. 在
UITextField
我将'退出时结束'与IBAction
并调用resignFirstResponder
方法。 How can i do this with UITextView
? 我怎么能用
UITextView
做到这一点?
The correct way to handle this is to add a done button in an inputAccessoryView
to the UITextView
. 处理此问题的正确方法是在
inputAccessoryView
完成按钮添加到UITextView
。 The inputAccessoryView
is the bar that sometimes appears above the keyboard. inputAccessoryView
是有时出现在键盘上方的栏。
In order to implement the inputAccessoryView
simply add this method (or a variation thereof) and call it in viewDidLoad
. 为了实现
inputAccessoryView
只需添加此方法(或其变体)并在viewDidLoad
调用它。
- (void)addInputAccessoryViewForTextView:(UITextView *)textView{
//Create the toolbar for the inputAccessoryView
UIToolbar* toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
[toolbar sizeToFit];
toolbar.barStyle = UIBarStyleBlackTranslucent;
//Add the done button and set its target:action: to call the method returnTextView:
toolbar.items = [NSArray arrayWithObjects:[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(returnTextView:)],
nil];
//Set the inputAccessoryView
[textView setInputAccessoryView:toolbar];
}
Then handel the button being pressed by implementing the action method you called with resignFirstResponder
. 然后通过实现使用
resignFirstResponder
调用的操作方法来按下按钮。
- (void) returnBreakdown:(UIButton *)sender{
[self.textView resignFirstResponder];
}
This should result in a working "Done" button appearing in a standard toolbar above the keyboard. 这应该会导致键盘上方标准工具栏中出现一个工作“完成”按钮。
I'm assuming by the "Done" button you mean the return key. 我假设通过“完成”按钮表示返回键。 It's not as intuitive as you might think.
它并不像你想象的那么直观。 This question covers it pretty well.
这个问题很好地涵盖了它。
如果你想能够使用你的返回键[[self view] endEditing: YES];
你可以将它添加到一个动作中[[self view] endEditing: YES];
Make sure you declare support for the UITextViewDelegate
protocol. 确保声明对
UITextViewDelegate
协议的支持。
@interface ...ViewController : UIViewController
` in .h file. @interface ...ViewController : UIViewController
在.h文件中。
In .m file, implement below method 在.m文件中,实现以下方法
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
if([text isEqualToString:@"\n"]) {
[textView resignFirstResponder];
return NO;
}
return YES; }
Here is the Swift version of the accessory "Done" button: 这是配件“完成”按钮的Swift版本:
@IBOutlet weak var textView: UITextView!
// In viewDidLoad()
let toolbar = UIToolbar()
toolbar.bounds = CGRectMake(0, 0, 320, 50)
toolbar.sizeToFit()
toolbar.barStyle = UIBarStyle.Default
toolbar.items = [
UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.FlexibleSpace, target: nil, action: nil),
UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Done, target: nil, action: "handleDone:")
]
self.textView.inputAccessoryView = toolbar
// -----------------
func handleDone(sender:UIButton) {
self.textView.resignFirstResponder()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.