Reputation: 9374
How to detect Keyboard height change, or keyboard change in iOS using Swift.
I'm able to add an observer for my app to detect wether the Keyboard is show or not using :
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(CommentView.keyboardWillShow(_:)), name: UIKeyboardWillShowNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(CommentView.keyboardWillHide(_:)), name: UIKeyboardWillHideNotification, object: nil)
and I'm changing my button position according to that :
func keyboardWillShow(notification: NSNotification) {
animateTextFieldWithKeyboard(notification)
}
func keyboardWillHide(notification: NSNotification) {
animateTextFieldWithKeyboard(notification)
}
func animateTextFieldWithKeyboard(notification: NSNotification) {
let userInfo = notification.userInfo!
let keyboardSize = (userInfo[UIKeyboardFrameEndUserInfoKey] as! NSValue).CGRectValue()
let duration = userInfo[UIKeyboardAnimationDurationUserInfoKey] as! Double
let curve = userInfo[UIKeyboardAnimationCurveUserInfoKey] as! UInt
// baseContraint is your Auto Layout constraint that pins the
// text view to the bottom of the superview.
if notification.name == UIKeyboardWillShowNotification {
if (BottomConstraint.constant == 0) {
BottomConstraint.constant += keyboardSize.height
}
// move up
}
else {
BottomConstraint.constant = 0
// move down
}
view.setNeedsUpdateConstraints()
let options = UIViewAnimationOptions(rawValue: curve << 16)
UIView.animateWithDuration(duration, delay: 0, options: options,
animations: {
self.view.layoutIfNeeded()
},
completion: nil
)
}
Everything is working fine as you can see in the screenshot :
But the problem comes when I change the keyboard type to Emoji for example. it hides my textField and my Button, so I would like to change the position of the button and the TextFiend according to the keyboard new height
Upvotes: 1
Views: 2210
Reputation: 41
I use this and all notifications are triggered.
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillShow(notification:)), name: NSNotification.Name.UIKeyboardWillShow, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillHide(notification:)), name: NSNotification.Name.UIKeyboardWillHide, object: nil)
NotificationCenter.default.addObserver(self, selector: #selector(keyboardWillChange(notification:)), name: NSNotification.Name.UIKeyboardWillChangeFrame, object: nil)
}
@objc func keyboardWillShow(notification: NSNotification) {
let keyboardSize = (notification.userInfo![UIKeyboardFrameBeginUserInfoKey] as! NSValue).cgRectValue
if keyboard == false{
keyboard = true
lastKeyboardHeight = keyboardSize.height
chatDetailView.frame.origin.y = chatDetailView.frame.origin.y-(keyboardSize.height-bottomMenu.frame.height)
}
}
@objc func keyboardWillChange(notification: NSNotification) {
let keyboardSize1 = (notification.userInfo![UIKeyboardFrameEndUserInfoKey] as! NSValue).cgRectValue
if keyboard == true && lastKeyboardHeight != keyboardSize1.height {
if lastKeyboardHeight < keyboardSize1.height{
let keyboardDifference: CGFloat = keyboardSize1.height-lastKeyboardHeight
chatDetailView.frame.origin.y -= keyboardDifference
} else {
let keyboardDifference: CGFloat = lastKeyboardHeight-keyboardSize1.height
chatDetailView.frame.origin.y += keyboardDifference
}
lastKeyboardHeight = keyboardSize1.height
}
}
@objc func keyboardWillHide(notification: NSNotification) {
if keyboard == true {
keyboard = false
chatDetailView.frame.origin.y = chatDetailView.frame.origin.y+(lastKeyboardHeight-bottomMenu.frame.height)
}
}
Upvotes: 1
Reputation: 131491
If you search on UIKeyboardWillShowNotification
in the Xcode docs you get to the section on UIWindow, which has a table of notifications at the end.
I suggest trying the UIKeyboardWillChangeFrameNotification
.
Time to find the answer: About 30 seconds.
Upvotes: 1