作者:郭雪峰Rongeqw_983 | 来源:互联网 | 2023-01-04 16:47
在iphone x中设置安全区域后。当键盘打开时,安全区域(键盘上方的白色区域)位于键盘上方,那么如何操作键盘?
键盘上方的白色区域。
处理键盘代码:-
func keyboardWillChangeFrameWithNotification(_ notification: Notification, showsKeyboard: Bool) {
let userInfo = notification.userInfo!
let animationDuration: TimeInterval = (userInfo[UIKeyboardAnimationDurationUserInfoKey] as! NSNumber).doubleValue
let keyboardScreenEndFrame = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
// keyboardRect = [self.view convertRect:keyboardRect fromView:nil];
let keyBoardRect = self.view.convert(keyboardScreenEndFrame, from:nil)
UIView.animate(withDuration: animationDuration, delay: 0, options: .beginFromCurrentState, animations: {
// Keyboard is going to appear. move composebar up
if showsKeyboard {
self.constraintBottomAttachmentView.cOnstant= keyBoardRect.size.height
} else { // Keyboard is going to disappear. Move composebar down.
self.constraintBottomAttachmentView.cOnstant= 0
}
self.view.layoutIfNeeded()
}, completion: { finished in
// Update the height of recipient bar.
self.updateRecipientBarMaxHeight()
})
}
iphone x中的键盘高度已增加,因此如果我从键盘高度中减去-34,则白色区域会减小。码:-
if showsKeyboard {
self.constraintBottomAttachmentView.cOnstant= keyBoardRect.size.height - self.view.safeAreaInsets.bottom /*(34)*/ }
那么,如何在不手动进行优化的情况下解决此问题呢?
1> 小智..:
您可以通过以下方法获取iPhone X底部空间的高度:
view.safeAreaInsets.bottom
请记住,这仅在iOS 11及更高版本中可用,因此您需要满足以下条件:
if #available(iOS 11.0, *) {
//Move Composebar for iOS 11
} else {
//Move Composebar for other Versions
}
在您的情况下,这看起来类似于:
if showsKeyboard {
if #available(iOS 11.0, *) {
self.constraintBottomAttachmentView.cOnstant= keyBoardRect.size.height - view.safeAreaInsets.bottom
} else {
self.constraintBottomAttachmentView.cOnstant= keyBoardRect.size.height
} else { // Keyboard is going to disappear. Move composebar down.
self.constraintBottomAttachmentView.cOnstant= 0
}
这对您有用吗?