Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle Keyboard with safe area in iphone x Using AutoLayout? [duplicate]

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 ?

like image 338
Amey Avatar asked Dec 18 '17 11:12

Amey


People also ask

What is the safeareainsets property on the iPhone X?

The safeAreaInsets property means that screen can be covered from all sides not only at the top and the bottom. When the iPhone X was presented it become clear why we would need the left and right insets. The iPhone X has the top and the bottom safe area insets in portrait orientation. Left, right and bottom insets in landscape orientation.

What is the iOS safe area?

iOS Safe Area. By Evgeny M., iOS Developer at Rosberry | by Evgeny Mikhaylov | @RosberryApps | Medium As you remember, in iOS 7 Apple introduced the topLayoutGuide and bottomLayoutGuide properties in UIViewController to describe a screen area that isn’t covered by any content (status bar, navigation bar, toolbar, tab bar, etc.)

How do I change the layout guide in Xcode?

To make updating your apps a bit easier, you have the option to activate the Use Safe Area Layout Guides setting for each of your Storyboards in Xcode. The Storyboard then replaces the top and bottom layout guides with a safe area and updates the constraints.

What is the orientation of the insets on the iPhone X?

The iPhone X has the top and the bottom safe area insets in portrait orientation. Left, right and bottom insets in landscape orientation. Let’s look at an example.


1 Answers

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?

like image 160
Toan Nguyen Avatar answered Sep 23 '22 08:09

Toan Nguyen