有时当self.view
上添加了scrollview
时,在
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
中调用 [self.view endEditing:YES]
时;并不能收起键盘,因为此时touch是触发在scrollView
上. 所以要想点击scrollView
的空白区域来收起键盘时,可以通过写一个scrollView
的扩展.代码如下:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
if (![self isMemberOfClass:[UIScrollView class]]) {
} else {
[[self nextResponder] touchesBegan:touches withEvent:event];
if ([super respondsToSelector:@selector(touchesBegan:withEvent:)]) {
[super touchesBegan:touches withEvent:event];
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
if (![self isMemberOfClass:[UIScrollView class]]) {
} else {
[[self nextResponder] touchesMoved:touches withEvent:event];
if ([super respondsToSelector:@selector(touchesBegan:withEvent:)]) {
[super touchesMoved:touches withEvent:event];
}
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
if (![self isMemberOfClass:[UIScrollView class]]) {
} else {
[[self nextResponder] touchesEnded:touches withEvent:event];
if ([super respondsToSelector:@selector(touchesBegan:withEvent:)]) {
[super touchesEnded:touches withEvent:event];
}
}
}
在需要此功能的页面控制器内添加以下代码:
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[self.view endEditing:YES];
}
网友评论