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 ?
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 ?
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.
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.)
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.
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.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With