I use below code to get keyboard height. Then use this height to calculate the frame of an UIView
to make sure this UIView
just on the top of the k
override func viewDidLoad() { super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow), name: UIResponder.keyboardWillShowNotification, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide), name: UIResponder.keyboardWillHideNotification, object: nil)
}
//Here keyboard is without any toolbar and suggestions boxes
@objc func keyboardWillShow(notification: NSNotification) {
if let keyboardSize = (notification.userInfo?[UIResponder.keyboardFrameBeginUserInfoKey] as? NSValue)?.cgRectValue {
let keyboardHeight = ((keyboardSize.height) > 240) ? 220 : (keyboardSize.height - 47)
self.view.layoutIfNeeded()
}
}
I also faced this problem. What I did, simply I check keyboard that is different on iPhone 7 and iPhone X. I just add some default margin with keyboard height. Now It is working fine on every device.
The keyboard height for both iPhone X and iPhone 8 should be correct. I can only guess that maybe you have a problem in your code for locating the "red part", and your assumption was that the keyboard height was incorrect whereas the problem was actually in the location of the view. Now - the reason for the location problem? My second guess is the red part is pinned to the bottom safe area layout guide, which on the iPhone 8 is 0, but on the iPhone X is inset 34 points.
See this image to illustrate both the difference in keyboard heights and that it is possible to draw a rectangle just above the keyboard using the keyboard height from the keyboard height reported in the NSNotification
for the keyboardWillShow
method:
If you want to share your code / constraints for positioning the red view, I should be able to show you the problem.
--Edit: For anyone interested to know how I extract the drew the red rectangle, I go into it in a blog post here.
While Craig's answer is correct, you might not want to pin your view to view.bottom or the bottomLayoutGuide rather than the safe area bottom (especially if your keyboard is not always open, and you don't want your views to cover the Home Indicator area).
Here is a fix for these cases. It deducts the height of the bottom inset of the safe area from the height of the keyboard:
var keyboardHeight = ... // Get the keyboard height from keyboard notification
if #available(iOS 11.0, *) {
let bottomInset = view.safeAreaInsets.bottom
keyboardHeight -= bottomInset
}
This works for every device and iOS version so far
- (void)keyboardWillShown:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGFloat kbHeight = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
CGFloat safeAreaBottomInset = 0;
if (@available(iOS 11.0, *)) {
safeAreaBottomInset = self.view.safeAreaInsets.bottom;
}
self.containerViewBottomConstraint.constant += (kbHeight - safeAreaBottomInset); //In my case I use a constraint to adapt the UI when the keyboard is presented
[self.view layoutIfNeeded];
}