旋转时关闭键盘

本文关键字:键盘 旋转 | 更新日期: 2023-09-27 18:21:09

我正在考虑向侦听中添加一个观察者,以获取方向更改通知。我想做的是,如果键盘当前已打开,则将其关闭。

我看不出能找到一个不涉及找到有焦点的个人控制的例子。

旋转时关闭键盘

假设您的编辑字段是self.view的子视图,则无论键盘的响应程序如何,都可以使用来关闭键盘

[self.view endEditing:YES];

只需从UITextField对象调用辞职FirstResponder,例如:

[textField resignFirstResponder];

您没有考虑实现观察器,而是考虑了以下UIViewController类引用:

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{  
    // dismiss keyboard (if it was presented via a textField, etc.
    [self.myTextField resignFirstResponder];
    // or
    // [self.view endEditing:YES]; 
    // you can check for specific orientation via UIInterfaceOrientation - for example
    // landscape
    if (toInterfaceOrientation != UIInterfaceOrientationPortrait && toInterfaceOrientation != UIInterfaceOrientationPortraitUpsideDown) {
        // do something
    }
}