[iOS]关闭/收起虚拟键盘的若干方法

一、点击Return按扭时收起键盘。注意:须要听从UITextFieldDelegate协议,并设置好代理spa

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return [textField resignFirstResponder];
}

或者代理

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return [textField endEditing:YES];
}

或者(这里的View必须是继承于UIControl)code

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return [self.view endEditing:YES];
}

或者继承

-(BOOL)textFieldShouldReturn:(UITextField *)textField {
    return [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
}

二、点击背景View收起键盘it

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    //以上4种收起方式中的任何一种
}


三、通用的收起键盘方式io

上面第4种收起键盘方式,能够用在任何地方的代码event

相关文章
相关标签/搜索