我的函数"keyboardWillShown“有问题。所以我想要的是,当我的菜单打开时,它就会出现在键盘上方。它在Iphone 8+,8,7,6上运行得很好。但是当我在模拟器上运行Iphone 11时,结果如下。
这是我的密码:
@objc func keyboardWillShown(notification: NSNotification) {
let info = notification.userInfo!
let keyboardFrame: CGRect = (info[UIResponder.keyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
UIView.animate(withDuration: 0.1, animations: { () -> Void in
self.keyboardConstrains.constant = keyboardFrame.size.height
})
}
呼叫功能
override func viewWillAppear(_ animated: Bool) {
NotificationCenter.default.addObserver( self, selector: #selector(keyboardWillShown(notification:)), name: UIResponder.keyboardWillShowNotification, object: nil )
}
override func viewWillDisappear(_ animated: Bool) {
NotificationCenter.default.removeObserver( self,name: UIResponder.keyboardWillShowNotification, object: nil )
}
发布于 2020-02-21 11:21:34
可以根据屏幕大小设置约束。
if self.view.height >= 800{ //For bigger screens (X ,11)
self.keyboardConstrains.constant = keyboardFrame.size.height - 50
} else {
self.keyboardConstrains.constant = keyboardFrame.size.height
}
发布于 2020-02-21 11:43:44
let window = UIApplication.shared.keyWindow
let bottomPadding = window?.safeAreaInsets.bottom
keyboardConstrains.constant = (keyboardSize?.height)! - (bottomPadding ?? 0)
这是因为所有的凹槽iPhone(X,Xr,iPhone11等)底部都有安全区域,所以键盘的高度是从主视图计算出来的,所以你要从安全区域设置"keyboardConstrains“,这就是为什么这个空间即将到来的原因。要删除这个空格,您必须检查底部是否有安全区域,而不是从键盘高度减去底部空间。
https://stackoverflow.com/questions/60337511
复制相似问题