方法一:
//1, 关闭键盘
[[[UIApplication sharedApplication] keyWindow] endEditing:YES];
想必大家都遇到过一个view上好多输入框,为了关闭弹出的软键盘要遍历然后调用resignFirstResponder的吧?今天竟然发现一个关键这个view上可能 打开的键盘很简单:[self.view endEditing:YES];
直接ok了。
方法二:
//2, 关闭键盘
[[self findFirstResponderBeneathView:self] resignFirstResponder];
[[self findFirstResponderBeneathView:self] becomeFirstResponder];//打开键盘
- (UIView*)findFirstResponderBeneathView:(UIView*)view
{
// Search recursively for first responder
for ( UIView *childView in view.subviews ) {
if ( [childView respondsToSelector:@selector(isFirstResponder)] && [childView isFirstResponder] )
return childView;
UIView *result = [self findFirstResponderBeneathView:childView];
if ( result )
return result;
}
return nil;
}
时间: 2024-10-12 08:21:17