[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

相關文章
相關標籤/搜索