【发布时间】:2011-08-08 08:38:41
【问题描述】:
我希望能够在用户点击键盘外的任意位置时关闭 iPhone 键盘。我该怎么做呢?我知道我需要解除响应者,但需要知道当用户敲击键盘空间时如何实现它。
【问题讨论】:
-
您可能会发现这很有用:github.com/michaeltyson/TPKeyboardAvoiding
标签: iphone objective-c cocoa-touch ios uitextfield
我希望能够在用户点击键盘外的任意位置时关闭 iPhone 键盘。我该怎么做呢?我知道我需要解除响应者,但需要知道当用户敲击键盘空间时如何实现它。
【问题讨论】:
标签: iphone objective-c cocoa-touch ios uitextfield
您需要在键盘下方添加一个透明的 UIVIew 作为子视图并在那里处理触摸,以关闭键盘。以下代码供您参考。
UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(overlayTouched:)];
gesture.delegate = self;
[(UITapGestureRecognizer *)gesture setNumberOfTouchesRequired:1];
UIView* trans = [[UIView alloc] initWithFrame:[[delegate view] bounds]];
[trans setOpaque:NO];
[trans setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin];
[trans setAlpha:0.3];
[trans setUserInteractionEnabled:YES];
trans.multipleTouchEnabled = YES;
[trans addGestureRecognizer:gesture];
[trans setBackgroundColor:[UIColor blackColor]];
[trans setTag:BLACK_SCREEN_VIEW];
【讨论】:
您需要添加 UITapGestureRecogniser 并将其分配给视图,然后在其选择器的文本字段上调用 resign first responder。
代码:
在viewDidLoad
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
在dismissKeyboard:
-(void)dismissKeyboard {
[aTextField resignFirstResponder];
}
(其中aTextField是负责键盘的文本字段)
选项 2
如果你负担不起添加手势识别器的费用,那么你可以试试这个
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch * touch = [touches anyObject];
if(touch.phase == UITouchPhaseBegan) {
[aTextField resignFirstResponder];
}
}
【讨论】:
touches.first 代替 [touches anyObject]。
我用过的最简单的解决方案是这样的:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
endEditing 命令可用于包含您的文本字段作为子视图的任何视图。此方法的另一个优点是您不需要知道哪个文本字段触发了键盘。因此,即使您有多个文本字段,也只需将此行添加到超级视图即可。
根据Apple的文档,我认为这个方法是专门用来解决这个问题的。
【讨论】:
UIScrollView 怎么样?
scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
添加一个 tapGesture 识别器,但确保 cancelsTouchesInView = NO
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(closeTextInput)];
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];
[tapGesture release];
【讨论】:
其他方法很简单:
创建你的UIView as UIControl in custom class in the interface builder,然后你可以在UIView : UIControl 的Touch up inside event 中附加一个IBAction 方法,然后你将[yourTextField resignFirstResponder] 放在IBAction method 中,如下所示:
- (IBAction) hideKeyboard: (id) sender
{
// If you have more than one textfield to dismiss, of course only can be active 1, but here you can't know who is it, because sender will be the UIView : UIControl
[alias resignFirstResponder];
[password resignFirstResponder];
}
然后,您还有其他选择,那就是在界面中输入您的文本字段the return key of the keyboard as Done(可以是您想要的任何一个,但完成它对此有好处,因为返回意味着对表单执行操作) builder,因此您可以按Done 并隐藏键盘,但在这种情况下,您必须将先前的 IBAction 方法附加到Did end on exit 事件。
通过这种方式,键盘将隐藏touching outside 或从键盘上触摸Done。
如果你想改进代码,如果只是将键盘触摸Done隐藏在键盘上,方法应该是:
// Attach all textFields here on Did end on exit event, will not work if touch outside the keyboard
- (IBAction) hideKeyboard: (id) sender
{
[sender resignFirstResponder];
}
【讨论】:
如果您使用UITableViewController 或有用户将与之交互的UITableView,则在您的 ViewDidLoad 中您可以简单地执行以下操作:
tableView.KeyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag;
注意:这是 Xamarin.iOS 语法。
【讨论】:
您需要添加一个 UITapGestureRecogniser 并将其分配给视图,然后在其选择器的文本字段上调用 resign first responder。
代码:
在 viewDidLoad 中
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
action:@selector(dismissKeyboard)];
[self.view addGestureRecognizer:tap];
在dismissKeyboard中:
-(void)dismissKeyboard {
[self.view endEditing:true];
}
【讨论】:
这是一个 Swift 4 解决方案:
let tap = UITapGestureRecognizer(target: self, action: #selector(self.dismissKeyboard))
self.view.addGestureRecognizer(tap)
还有dismissKeyboard
@objc func dismissKeyboard() {
self.view.endEditing(true)
}
【讨论】:
如果您不想添加手势识别器,也可以使用 touchesBegin 方法
Swift 4 中的代码
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { view.endEditing(true) }
【讨论】: