在IB的库中,介绍告诉我们,当return按下键时,键盘UITextView
将消失.但实际上return密钥只能作为'\n'.
我可以添加一个按钮并[txtView resignFirstResponder]
用来隐藏键盘.
但有没有办法return在键盘中添加键的动作,以便我不需要添加UIButton
?
想我会在这里发布片段代替:
确保声明对UITextViewDelegate
协议的支持.
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if([text isEqualToString:@"\n"]) { [textView resignFirstResponder]; return NO; } return YES; }
Swift 4.0更新:
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { if text == "\n" { textView.resignFirstResponder() return false } return true }
UITextView
没有任何方法将在用户点击返回键时调用.如果您希望用户只能添加一行文本,请使用UITextField
.点击返回并将键盘隐藏为a UITextView
不符合界面指南.
即使这样,如果你想这样做,实现textView:shouldChangeTextInRange:replacementText:
方法,UITextViewDelegate
并检查替换文本是否\n
隐藏键盘.
可能还有其他方法,但我不知道.
我知道这已经回答了,但我真的不喜欢使用新行的字符串文字,所以这就是我所做的.
- (BOOL)textView:(UITextView *)txtView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if( [text rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet]].location == NSNotFound ) { return YES; } [txtView resignFirstResponder]; return NO; }
Swift 4.0更新:
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool { if (text as NSString).rangeOfCharacter(from: CharacterSet.newlines).location == NSNotFound { return true } txtView.resignFirstResponder() return false }
我知道这已经被回答了很多次,但这是我的两分钱.
我发现答案samvermette和ribeto真正有用的,并且还发表谈话牛魔王在ribeto的答案.但这些方法存在问题.但问题亚光提到在samvermette的答案,那就是如果用户想要的东西贴里面有一条线突破时,键盘会隐藏不粘贴任何东西.
所以我的方法是上面提到的三个解决方案的混合,只检查当字符串的长度为1时输入的字符串是否为新行,因此我们确保用户输入而不是粘贴.
这是我做的:
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { NSRange resultRange = [text rangeOfCharacterFromSet:[NSCharacterSet newlineCharacterSet] options:NSBackwardsSearch]; if ([text length] == 1 && resultRange.location != NSNotFound) { [textView resignFirstResponder]; return NO; } return YES; }
更优雅的方法是当用户轻敲键盘框架外的某个位置时关闭键盘.
首先,将ViewController的视图设置为UIBuilder中的标识检查器中的"UIControl"类.控制 - 将视图拖动到ViewController的头文件中,并将其作为Touch Up Inside事件的动作链接,例如:
ViewController.h
-(IBAction)dismissKeyboardOnTap:(id)sender;
在ViewController主文件中,ViewController.m:
-(IBAction)dismissKeyboardOnTap:(id)sender { [[self view] endEditing:YES]; }
您可以使用类似技术进行双击或长按.您可能需要将ViewController设置为UITextViewDelegate并将TextView连接到ViewController.此方法适用于UITextView和UITextField.
资料来源:Big Nerd Ranch
编辑:我还想补充说,如果您使用的是UIScrollView,上述技术可能无法通过Interface Builder轻松完成.在这种情况下,您可以使用UIGestureRecognizer并在其中调用[[self view] endEditing:YES]方法.一个例子是:
-(void)ViewDidLoad{ .... UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tap:)]; [self.view addGestureRecognizer: tapRec]; .... } -(void)tap:(UITapGestureRecognizer *)tapRec{ [[self view] endEditing: YES]; }
当用户点击键盘外部并且没有点击入口空间时,键盘将被解除.
在视图控制器中添加此方法.
斯威夫特:
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool { if text == "\n" { textView.resignFirstResponder() return false } return true }
这个方法也对你有所帮助:
/** Dismiss keyboard when tapped outside the keyboard or textView :param: touches the touches :param: event the related event */ override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { if let touch = touches.anyObject() as? UITouch { if touch.phase == UITouchPhase.Began { textField?.resignFirstResponder() } } }
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text { if([text isEqualToString:@"\n"]) [textView resignFirstResponder]; return YES; } yourtextView.delegate=self;
还添加 UITextViewDelegate
不要忘记确认协议
如果您没有添加if([text isEqualToString:@"\n"])
,则无法编辑
使用uitextview时还有另一种解决方案,你可以在"textViewShouldBeginEditing"中添加工具栏作为InputAccessoryView,从这个工具栏的完成按钮可以解除键盘,其代码如下:
在viewDidLoad中
toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 44)]; //toolbar is uitoolbar object toolBar.barStyle = UIBarStyleBlackOpaque; UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(btnClickedDone:)]; [toolBar setItems:[NSArray arrayWithObject:btnDone]];
在textviewdelegate方法中
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView { [textView setInputAccessoryView:toolBar]; return YES; }
在工具栏中的Button Done的操作如下:
-(IBAction)btnClickedDone:(id)sender { [self.view endEditing:YES]; }
我发现josebama的答案是这个帖子中最完整,最干净的答案.
下面是它的Swift 4语法:
func textView(_ textView: UITextView, shouldChangeTextIn _: NSRange, replacementText text: String) -> Bool { let resultRange = text.rangeOfCharacter(from: CharacterSet.newlines, options: .backwards) if text.count == 1 && resultRange != nil { textView.resignFirstResponder() // Do any additional stuff here return false } return true }
使用导航控制器来托管一个条以关闭键盘:
在.h文件中:
UIBarButtonItem* dismissKeyboardButton;
在.m文件中:
- (void)viewDidLoad { dismissKeyboardButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(dismissKeyboard)]; } -(void)textViewDidBeginEditing:(UITextView *)textView { self.navigationItem.rightBarButtonItem = dismissKeyboardButton; } -(void)textFieldDidBeginEditing:(UITextField *)textField { self.navigationItem.rightBarButtonItem = dismissKeyboardButton; } -(void)dismissKeyboard { [self.textField resignFirstResponder]; [self.textView resignFirstResponder]; //or replace this with your regular right button self.navigationItem.rightBarButtonItem = nil; }
迅速
func textView(textView: UITextView, shouldChangeTextInRange range: NSRange, replacementText text: String) -> Bool { if text == "\n" { textView.resignFirstResponder() } return true }
就像对samvermette的暗淡评论一样,我也不喜欢检测"\n"的想法.在UITextView中出现"return"键是有原因的,那就是当然要转到下一行.
在我看来,最好的解决方案是模仿iPhone消息应用程序 - 即在键盘上添加工具栏(和按钮).
我从以下博客文章中获得了代码:
http://www.iosdevnotes.com/2011/02/iphone-keyboard-toolbar/
脚步:
- 将工具栏添加到XIB文件 - 将高度设置为460
- 添加工具栏按钮项(如果尚未添加).如果需要右对齐,还可以向XIB添加灵活的条形按钮项,并移动工具栏按钮项
- 创建将按钮项链接到resignFirstResponder的操作,如下所示:
- (IBAction)hideKeyboard:(id)sender { [yourUITextView resignFirstResponder]; }
-然后:
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; } - (void)viewWillDisappear:(BOOL)animated { [super viewWillDisappear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; } - (void)keyboardWillShow:(NSNotification *)notification { [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.3]; CGRect frame = self.keyboardToolbar.frame; frame.origin.y = self.view.frame.size.height - 260.0; self.keyboardToolbar.frame = frame; [UIView commitAnimations]; } - (void)keyboardWillHide:(NSNotification *)notification { [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.3]; CGRect frame = self.keyboardToolbar.frame; frame.origin.y = self.view.frame.size.height; self.keyboardToolbar.frame = frame; [UIView commitAnimations]; }
在viewDidLoad中添加一个观察者
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(textViewKeyPressed:) name: UITextViewTextDidChangeNotification object: nil];
然后使用选择器检查"\n"
-(void) textViewKeyPressed: (NSNotification*) notification { if ([[[notification object] text] hasSuffix:@"\n"]) { [[notification object] resignFirstResponder]; } }
它确实使用"\n"而不是专门检查返回键,但我认为这没关系.
UPDATE
请参阅下面使用ribto的答案[NSCharacterSet newlineCharacterSet]
代替\n
刚刚以不同的方式解决了这个问题.
创建一个将放置在后台的按钮
从"属性"检查器中,将按钮类型更改为"自定义",并使按钮透明.
展开按钮以覆盖整个视图,并确保按钮位于所有其他对象的后面.简单的方法是将按钮拖动到视图中列表视图的顶部
控制将按钮拖动到viewController.h
文件并创建一个动作(已发送事件:内部触摸),如:
(IBAction)ExitKeyboard:(id)sender;
在ViewController.m
应该是这样的:
(IBAction)ExitKeyboard:(id)sender { [self.view endEditing:TRUE]; }
运行应用程序,当您单击TextView时,键盘将消失