答案 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)