11

在 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)*/ } 

那么如何在不手动执行此操作并以优化方式解决此问题?

4

1 回答 1

10

您可以通过以下方式获取 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
}

这对你有用吗?

于 2018-01-05T02:00:03.157 回答