IT虾米网

iOS点击非输入框时隐藏键盘

itxm 2018年06月23日 手机开发 1072 0
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField 
{ 
    UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(done:)]; 
    tapGestureRecognizer.numberOfTapsRequired = 1; 
    [self.view addGestureRecognizer: tapGestureRecognizer];   //只需要点击非文字输入区域就会响应hideKeyBoard 
    [tapGestureRecognizer release]; 
    return YES; 
} 
 
-(void)done:(id)sender 
{ 
    for (UIView *view in self.view.subviews) { 
        if ([view isKindOfClass:[UITextField class]]) { 
            [view resignFirstResponder]; 
        } 
    } 
}

评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!