我正在使用
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
self.view.endEditing(true)
}
为了在用户点击某个地方时关闭键盘,但它不起作用。而且我没有任何错误
我的VC已经构建完毕:查看&gt; <视觉效果视图&gt;视图&gt;滚动视图&gt; uiButton (它覆盖屏幕并作为上一个VC的退出/后退按钮)&gt;可设计视图(这是我的登录表单所在的位置)
答案 0 :(得分:2)
在界面构建器中,在点击手势后拖动您希望它关闭键盘的视图上的tap gesture recognizer
,或者您可以按代码添加手势:
-(void)viewDidLoad
{
[super viewDidLoad];
UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tappedOutside:)];
gestureRecognizer.cancelsTouchesInView = NO;
[scrollView addGestureRecognizer:gestureRecognizer];
}
将IBAction
添加到tap gesture recognizer
,例如- (IBAction)tappedOutside:(id)sender;
在实施文件中添加:
-(IBAction)tappedOutside:(id)sender {
[self.view endEditing:YES];
}
夫特:
var tap : UITapGestureRecognizer = UITapGestureRecognizer(target: self,action: "dismissKeyboard")
self.view.addGestureRecognizer(tap)
func dismissKeyboard() {
self.view.endEditing(true)
}
答案 1 :(得分:1)
使用打开该键盘的textField 并尝试
textField.resignFirstResponder()
答案 2 :(得分:0)
首先,将代理人添加到您的文字归档
yourtextfiled.delegate=self
其次,为您的UIScrollview和View
设置用户互动功能 override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
yourtextfiled.resignFirstResponder()
}