折叠键盘,工具栏

时间:2017-06-22 14:50:28

标签: ios swift mobile keyboard

collapsed keyboard

如何检查键盘何时完全显示以及何时崩溃?也许我可以检查它的高度?

我现在检查userInfo notification,我发现解决问题没什么用。

1 个答案:

答案 0 :(得分:1)

只需订阅通知:

NotificationCenter.default.addObserver(self, selector: 
#selector(self.adjustForKeyboard), name: 
NSNotification.Name.UIKeyboardDidShow, object: nil)

NotificationCenter.default.addObserver(self, selector: 
#selector(self.adjustForKeyboard), name: 
NSNotification.Name.UIKeyboardDidHide, object: nil)

并粘贴下一个方法

func adjustForKeyboard(notification: NSNotification) {
    let userInfo = notification.userInfo!

    if  let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as? NSValue)?.cgRectValue {
        let keyboardViewEndFrame = delegate.getView().convert(keyboardScreenEndFrame, from: delegate.getView().window)

        var customInset = UIEdgeInsets.zero            
        if notification.name == NSNotification.Name.UIKeyboardDidHide {
            customInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
        } else {
            customInset = UIEdgeInsets(top: 0, left: 0, bottom: keyboardViewEndFrame.height, right: 0)
        }

        scrollView.contentInset = customInset
        scrollView.scrollIndicatorInsets = customInset
    }
}
当你离开这个VC时,不要忘记删除观察者

NotificationCenter.default.removeObserver(self)