10

After Setting Safe Area in iphone x . When Keyboard opens the safe area (The white area above keyboard ) comes above the keyboard so how to handle the keyboard ?

enter image description here

White Area above The Keyboard .

handle keyboard Code :-

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()
        })
    }

keyboard height has increased in iphone x so if i subtract - 34 from keyboard height the white area decreases . Code:-

if showsKeyboard {
                  self.constraintBottomAttachmentView.constant =  keyBoardRect.size.height - self.view.safeAreaInsets.bottom /*(34)*/ } 

So how to solve this issue without manually doing this and in an optimised way ?

Amey
  • 795
  • 8
  • 31

1 Answers1

10

You can get the height of the space at the bottom on an iPhone X with:

view.safeAreaInsets.bottom

Keep in mind that this is only available in iOS 11 and above so you need this condition:

if #available(iOS 11.0, *) {
//Move Composebar for iOS 11
} else {
//Move Composebar for other Versions
}

In your case this would look similar to this:

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
}

Does this work for you?

Toan Nguyen
  • 129
  • 6
  • i have already written this code above i want optimised way without manually updating constraint from code with autolayout. – Amey Jan 05 '18 at 06:59