I am trying to bring my views up when the keyboard appears by modifying the bottom constrain to the keyboard height. But the keyboard height returned to me is varying.
When I tap on a textfield in the simulator, the keyboard height was 302
. When I try to toggle on and off the software keyboard, it shows 260
when the keyboard appears. Why is this happening?
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(FriendsViewController.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
func keyboardWillShow(notification: NSNotification) {
print("Keyboard appearing")
guard let keyboardHeight = (notification.userInfo! as NSDictionary).objectForKey(UIKeyboardFrameBeginUserInfoKey)?.CGRectValue.size.height else {
return
}
bottomConstraint.constant = keyboardHeight
print("keyboard height : \(keyboardHeight)")
self.view.layoutIfNeeded()
}
Height of 260
is actually the correct height, as it adjusted my views perfectly. With a height of 302
my views get offset too far up.
The layout of my view is. UITextField
on the top and followed by a UITableView
below it.
Modified answer of Matt with reason,
He is right you need to use UIKeyboardFrameEndUserInfoKey
instead of UIKeyboardFrameBeginUserInfoKey
because
UIKeyboardFrameEndUserInfoKey
gives you the final height according the prefences you have set in your setting.
UIKeyboardFrameEndUserInfoKey
returns you height two times first one is without the language prediction as you can see above the keyboard and next one with predicate if it is activated from the setting but UIKeyboardFrameBeginUserInfoKey
returns without prediction bar.
Height on toggle in iPhone 5s
The problem is that you are looking at UIKeyboardFrameBeginUserInfoKey
. What you want to look at is UIKeyboardFrameEndUserInfoKey
.
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