我试图实现一个通知,提醒键盘将出现并且应该随着键盘高度向上移动的视图。
func subscribeToKeyboardNotifications() {
NotificationCenter.default.addObserver(self, selector: #selector(KeyboardWillShow(_:)), name: .UIKeyboardWillShow, object: nil)
}
这是KeyboardWillShow方法体:
@objc func KeyboardWillShow(_ notification: Notification) {
view.frame.origin.y -= getKeyBoardHeight(notification)
}
和getKeyBoardHeight函数:
func getKeyBoardHeight(_ notification: Notification) -> CGFloat {
let userinfo = notification.userInfo
let keyboardsize = userinfo![UIKeyboardFrameEndUserInfoKey] as! NSValue
return keyboardsize.cgRectValue.height
}
为什么当我在模拟器中打开键盘时,我的应用程序会完全移出视线?