1

如何在 iPad 上收听浮动键盘显示和隐藏?UIKeyboardWillShowNotification或者UIKeyboardWillHideNotification不会被召回。主要代码如下:

- (void)viewDidLoad() {
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide) name:UIKeyboardWillHideNotification object:nil];
}

- (void)keyboardWillShow:(NSNotification *)note {
    // NOT called back
}

- (void)keyboardWillHide {
    // NOT called back
}
4

1 回答 1

0

标准键盘willShowwillHide不适用于 iPad 上的浮动键盘。相反,我们可以观察willChangeFrame通知,并查看开始和结束帧来确定键盘是出现还是隐藏。

这是一个快速的例子:

func viewDidLoad() {
    NotificationCenter.default.addObserver(self, selector: #selector(self.keyboardFrameWillChange), name: NSNotification.Name(rawValue: UIResponder.keyboardWillChangeFrameNotification.rawValue), object: nil)
}

@objc func keyboardFrameWillChange() {
    let userInfo = notification.userInfo!
    let keyBoardEndRect = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as! CGRect
    let keyBoardBeginRect = userInfo[UIResponder.keyboardFrameBeginUserInfoKey] as! CGRect

    if (keyBoardBeginRect.equalTo(CGRect.zero)) {
        // Floating keyboard will show        
    }

    if (keyBoardEndRect.equalTo(CGRect.zero)) {
        // Floating keyboard will hide        
    }
}
于 2020-12-08T04:51:42.987 回答