iOS - Dismiss keyboard when touching outside of UITextField
我想知道,当用户接触到
你将需要添加到
的代码:
在viewdidload
1 2 3 | UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)]; [self.view addGestureRecognizer:tap]; |
在dismisskeyboard:
1 2 3 4 | -(void)dismissKeyboard { [aTextField resignFirstResponder]; } |
(这是textfield WHERE
3版本看起来像斯威夫特
1 2 | let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard (_:))) self.view.addGestureRecognizer(tapGesture) |
dismisskeyboard for
1 2 3 | func dismissKeyboard (_ sender: UITapGestureRecognizer) { aTextField.resignFirstResponder() } |
一泥上几个答案。
在initialized Ivar是得到使用在
1 2 3 4 5 6 7 8 9 10 | UIGestureRecognizer *tapper; - (void)viewDidLoad { [super viewDidLoad]; tapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; tapper.cancelsTouchesInView = NO; [self.view addGestureRecognizer:tapper]; } |
什么是当前的编辑:"解散
1 2 3 4 | - (void)handleSingleTap:(UITapGestureRecognizer *) sender { [self.view endEditing:YES]; } |
检查这个,这将是最简单的方式做,
1 2 3 | -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [self.view endEditing:YES];// this will do the trick } |
或
这包括滚动条自动滚动图书馆想把手,水龙头的空间隐藏键盘,等等……
http:/ / / / tpkeyboardavoiding github.com michaeltyson
我看到一些人是有问题的
1 | [tap setCancelsTouchesInView:NO]; |
我本可以不使用现有的按钮到"德米特里sitnikov安静工作的方法。
在这里读吗
我不知道它如何会与滚动条滚动条问题,我有一个湖,但我们其他人可能会运行到一个相同的场景。
它是更好地让你的
1 2 3 | - (IBAction)dismissKeyboard:(id)sender { [aTextBox resignFirstResponder]; } |
swift版本,与其他元素(如
1 2 3 4 5 6 7 | override func viewDidLoad() { super.viewDidLoad() let tapper = UITapGestureRecognizer(target: self, action:#selector(endEditing)) tapper.cancelsTouchesInView = false view.addGestureRecognizer(tapper) } |
本:我知道如何这是一个旧的邮件。帮助的人:)
1 2 3 4 5 6 7 8 9 10 11 | - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { NSArray *subviews = [self.view subviews]; for (id objects in subviews) { if ([objects isKindOfClass:[UITextField class]]) { UITextField *theTextField = objects; if ([objects isFirstResponder]) { [theTextField resignFirstResponder]; } } } } |
斯威夫特4
使用此扩展方法设置您的
1 2 3 4 | override func viewDidLoad() { super.viewDidLoad() self.setupHideKeyboardOnTap() } |
即使轻敲
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import UIKit extension UIViewController { /// Call this once to dismiss open keyboards by tapping anywhere in the view controller func setupHideKeyboardOnTap() { self.view.addGestureRecognizer(self.endEditingRecognizer()) self.navigationController?.navigationBar.addGestureRecognizer(self.endEditingRecognizer()) } /// Dismisses the keyboard from self.view private func endEditingRecognizer() -> UIGestureRecognizer { let tap = UITapGestureRecognizer(target: self.view, action: #selector(self.view.endEditing(_:))) tap.cancelsTouchesInView = false return tap } } |
这是一个很好的通用解决方案:
ObjultC:
1 2 3 | - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; } |
Swift:
1 2 3 | override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { self.view.endEditing(true) } |
基于@icodebugster解决方案:https://stackoverflow.com/a/18756253/417652
我认为最简单的(和最佳)的方式这样做是你的全局视图和使用收藏指正
这是更好的比使用
因此,它是更好的比你可能
Swift 4单列
1 | view.addGestureRecognizer(UITapGestureRecognizer(target: view, action: #selector(UIView.endEditing(_:)))) |
您可以使用Xcode 6及更高版本中的故事板执行此操作:
< BR>
创建隐藏键盘的操作将其添加到视图控制器使用的类的头文件中:
1 2 3 4 5 | @interface TimeDelayViewController : UIViewController <UITextFieldDelegate> - (IBAction)dissmissKeyboardOnTap:(id)sender; @end |
然后将其添加到同一个ViewController的实现文件中:
1 2 3 | - (IBAction)dissmissKeyboardOnTap:(id)sender{ [[self view]endEditing:YES]; } |
现在,这将是故事板场景(即,ViewController)的"已接收操作"之一:
< BR>
将操作连接到用户事件现在,您需要将此操作连接到触摸键盘的用户手势。
重要提示-您需要将故事板中包含的"uiview"转换为uicontrol,以便它可以接收事件。从视图控制器场景层次中选择视图:
…并更改其类别:
现在,从场景的"已接收操作"旁边的小圆圈拖动到场景的"空"部分(实际上,您正在将"已接收操作"拖动到uicontrol)。将向您显示一系列事件,您可以将操作连接到:
选择"内部补漆"选项。现在,您已经将创建的IBaction与用户触摸键盘的操作挂钩。当用户点击键盘时,它将被隐藏。
(注意:要将操作挂接到事件上,还可以从接收到的操作直接拖动到视图控制器层次结构中的uicontrol上。它在层次结构中显示为"控件"。)
这必须是通过触摸外部隐藏键盘的最简单方法:
1 2 3 | - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; } |
(从用户点击文本字段外的其他区域时如何关闭键盘?)
如果我有你的权利,你要在外接键盘resign野生
试试这个;
- 以全球textfield,让它
reftextField 呼叫 现在,在一场
textFieldDidBeginEditing 集参考文本1
2
3- (void) textFieldDidBeginEditing:(UITextField *)textField{
reftextField = textField;
}现在你可以使用任何的有趣的时钟(按钮,按钮上添加透明的印象开始编辑(一)
1
2
3- (void)dismissKeyboard {
[reftextField resignFirstResponder];
}resigning按钮或尝试做这个。
1
2
3
4
5//for resigning on done button
- (BOOL) textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
如果视图完全嵌入到
1 2 | tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag; tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeInteractive; |
前者会在滚动表视图时使键盘离开屏幕,后者会像股票消息应用程序一样隐藏键盘。
Note that these are are available on iOS 7.0 or above.
这里有很多关于使用
1 2 3 4 5 | - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch { BOOL touchViewIsButton = [touch.view isKindOfClass:[UIButton class]]; BOOL touchSuperviewIsTextField = [[touch.view superview] isKindOfClass:[UITextField class]]; return !(touchViewIsButton && touchSuperviewIsTextField); } |
这不是最强大的解决方案,但它对我很有效。
您可以为uiview创建类别并按如下方式覆盖touchebegan meathod。
它对我来说很好,而且是这个问题的集中解决方案。
1 2 3 4 5 6 7 8 9 | #import"UIView+Keyboard.h" @implementation UIView(Keyboard) - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.window endEditing:true]; [super touchesBegan:touches withEvent:event]; } @end |
@jensen2k答案的swift版本:
1 2 3 4 5 6 | let gestureRecognizer : UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action:"dismissKeyboard") self.view.addGestureRecognizer(gestureRecognizer) func dismissKeyboard() { aTextField.resignFirstResponder() } |
一班轮
1 | self.view.addTapGesture(UITapGestureRecognizer.init(target: self, action:"endEditing:")) |
只是添加到列表的版本这里我在外面如何解散一触摸键盘。
viewdidload:
1 2 | UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; [self.view addGestureRecognizer:singleTap]; |
的地方:
1 2 3 4 | -(void)handleSingleTap:(UITapGestureRecognizer *)sender{ [textFieldName resignFirstResponder]; puts("Dismissed the keyboard"); } |
最简单和最短的方法之一是将此代码添加到您的
1 2 3 | [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self.view action:@selector(endEditing:)]]; |
我以巴里为例进行了新的开发。效果很好!但我不得不做一个小小的修改,只需要对正在编辑的文本字段取消键盘。
因此,我在Barry示例中添加了以下内容:
1 2 3 4 5 6 7 8 | - (void) textFieldDidBeginEditing:(UITextField *)textField { _textBeingEdited = textField; } -(void) textFieldDidEndEditing:(UITextField *)textField { _textBeingEdited = nil; } |
另外,我更改了hidekeyboard方法,如下所示:
1 2 3 4 5 6 7 | - (IBAction)hideKeyboard:(id)sender { // Just call resignFirstResponder on all UITextFields and UITextViews in this VC // Why? Because it works and checking which one was last active gets messy. //UITextField * tf = (UITextField *) sender; [_textBeingEdited resignFirstResponder]; } |
我想我在这里回答和没有运气。我有什么,我总是
这是什么,最终解决问题:
艾薇:有
1 | UITapGestureRecognizer *_keyboardDismissGestureRecognizer; |
当一场:设置文本编辑:手势,语音门户
1 2 3 4 5 6 7 8 9 10 11 12 | - (void) textFieldDidBeginEditing:(UITextField *)textField { if(_keyboardDismissGestureRecognizer == nil) { _keyboardDismissGestureRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissKeyboard)] autorelease]; _keyboardDismissGestureRecognizer.cancelsTouchesInView = NO; [self.view addGestureRecognizer:_keyboardDismissGestureRecognizer]; } } |
是你哄骗的话:"
1 2 3 4 5 6 7 8 9 10 11 12 | - (void) dismissKeyboard { [self performSelector:@selector(dismissKeyboardSelector) withObject:nil afterDelay:0.01]; } - (void) dismissKeyboardSelector { [self.view endEditing:YES]; [self.view removeGestureRecognizer:_keyboardDismissGestureRecognizer]; _keyboardDismissGestureRecognizer = nil; } |
我猜它只是一些关于
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | - (void)viewDidLoad { [super viewDidLoad]; UITapGestureRecognizer *singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)]; [singleTapGestureRecognizer setNumberOfTapsRequired:1]; [singleTapGestureRecognizer requireGestureRecognizerToFail:singleTapGestureRecognizer]; [self.view addGestureRecognizer:singleTapGestureRecognizer]; } - (void)handleSingleTap:(UITapGestureRecognizer *)recognizer { [self.view endEditing:YES]; [textField resignFirstResponder]; [scrollView setContentOffset:CGPointMake(0, -40) animated:YES]; } |
发送消息到textfiled
本厂
在这个例子中,是唯一的uitextfield atextfield…如果有人或uitextviews位,有一个小更多的待办事项。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | // YourViewController.h // ... @interface YourViewController : UIViewController /* some subclass of UIViewController */ <UITextFieldDelegate> // <-- add this protocol // ... @end // YourViewController.m @interface YourViewController () @property (nonatomic, strong, readonly) UITapGestureRecognizer *singleTapRecognizer; @end // ... @implementation @synthesize singleTapRecognizer = _singleTapRecognizer; // ... - (void)viewDidLoad { [super viewDidLoad]; // your other init code here [self.view addGestureRecognizer:self.singleTapRecognizer]; { - (UITapGestureRecognizer *)singleTapRecognizer { if (nil == _singleTapRecognizer) { _singleTapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapToDismissKeyboard:)]; _singleTapRecognizer.cancelsTouchesInView = NO; // absolutely required, otherwise"tap" eats events. } return _singleTapRecognizer; } // Something inside this VC's view was tapped (except the navbar/toolbar) - (void)singleTapToDismissKeyboard:(UITapGestureRecognizer *)sender { NSLog(@"singleTap"); [self hideKeyboard:sender]; } // When the"Return" key is pressed on the on-screen keyboard, hide the keyboard. // for protocol UITextFieldDelegate - (BOOL)textFieldShouldReturn:(UITextField*)textField { NSLog(@"Return pressed"); [self hideKeyboard:textField]; return YES; } - (IBAction)hideKeyboard:(id)sender { // Just call resignFirstResponder on all UITextFields and UITextViews in this VC // Why? Because it works and checking which one was last active gets messy. [aTextField resignFirstResponder]; NSLog(@"keyboard hidden"); } |
1 2 3 4 5 6 7 | override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { if let touch = touches.first{ view.endEditing(true) } } |
在这种情况下,可以使用scrollview并在scrollview中添加到
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | import UIKit class ViewController: UIViewController { @IBOutlet weak var scrollView: UIScrollView! @IBOutlet weak var textField: UITextField! override func viewDidLoad() { super.viewDidLoad() let tapGesture = UITapGestureRecognizer(target: self, action: #selector(ViewController.tap(_:))) view.addGestureRecognizer(tapGesture) // Do any additional setup after loading the view, typically from a nib. } func tap(gesture: UITapGestureRecognizer) { textField.resignFirstResponder() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } |
你的故事板是这样看的。
在viewcontroller.m文件中添加此代码:
1 2 3 4 | -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self.view endEditing:YES]; } |
设置视图中的文本字段委托已加载:
1
2
3
4
5override func viewDidLoad()
{
super.viewDidLoad()
self.userText.delegate = self
}添加此函数:
1
2
3
4
5func textFieldShouldReturn(userText: UITextField!) -> Bool
{
userText.resignFirstResponder()
return true;
}
对于那些在这方面挣扎的人来说。这是Jensen2K接受的答案,但用的是Swift。
斯威夫特2.3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | override func viewDidLoad() { //..... let viewTapGestureRec = UITapGestureRecognizer(target: self, action: #selector(handleViewTap(_:))) //this line is important viewTapGestureRec.cancelsTouchesInView = false self.view.addGestureRecognizer(viewTapGestureRec) //..... } func handleViewTap(recognizer: UIGestureRecognizer) { myTextField.resignFirstResponder() } |
通过单击uitextfield外部,可以使用uitapGestureReconger方法解除键盘。每当用户单击uitextfield外部时,使用此方法,键盘将被关闭。下面是使用它的代码段。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissk)]; [self.view addGestureRecognizer:tap]; //Method - (void) dismissk { [abctextfield resignFirstResponder]; [deftextfield resignFirstResponder]; } |
所以我只需要解决这个问题,而之前的答案都不是现成的。我的情况:一个
我做了什么:
1)在我的视图控制器中实现自定义触摸处理程序。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { if searchBar.isFirstResponder() { // This causes the first responder, whoever it is, to resign first responder, and hide the keyboard. // We also"eat" the touch here and not allow it to propagate further. view.endEditing(true) } else { // OK to propagate the touch super.touchesBegan(touches, withEvent: event) } } |
2)添加了一些委托方法(我的方法用于uisearchbar,但uiextfield有类似的方法)。下面代码中的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | func searchBarTextDidBeginEditing(searchBar: UISearchBar) { controlContainerView.userInteractionEnabled = false someButton.userInteractionEnabled = false } func searchBarTextDidEndEditing(searchBar: UISearchBar) { searchBar.resignFirstResponder() // Done editing: enable the other controls again. controlContainerView.userInteractionEnabled = false someButton.userInteractionEnabled = false } |
在视图加载中斯威夫特4.2这样的代码
1 2 3 4 5 | let viewTap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action:#selector(dismissKeyboard)) view.addGestureRecognizer(viewTap) @objc func dismissKeyboard() { view.endEditing(true) } |
只需在.m文件中使用此代码,当用户点击文本字段外部时,它将退出文本字段。
1 2 3 | -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ [textfield resignFirstResponder]; } |
1 2 3 4 | override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { super.view.endEditing(true) super.touchesBegan(touches, withEvent: event) } |