I'm trying to keep a text field sitting atop the keyboard in iOS 8. But when a user swipes up or down the top of the keyboard to show or dismiss iOS 8 word suggestions, I need to a notification of the new height of the keyboard so I can move my text field up or down by that height.
How can I accomplish this?
Thanks!
You can register for UIKeyboardDidShowNotification and then get the keyboard frame from the notification with UIKeyboardFrameEndUserInfoKey.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleKeyboardDidShowNotification:)
name:UIKeyboardDidShowNotification
object:nil];
- (void)handleKeyboardDidShowNotification:(NSNotification *)notification
{
NSDictionary* info = [notification userInfo];
CGSize keyboardSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
// Move your textview into view here
}
This notification will be sent even in the event that the keyboard is already showing and is just going to change size, so you'll get it whenever you swipe up or down on the top of the keyboard.
Here's some code from a custom inputView I built. It even handles the animation curve for your custom view so that it matches the velocity of the keyboard and moves along with it.
The notification will fire when suggestions are shown/hidden.
- (void)registerForKeyboardNotifications {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillShow:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillHide:)
name:UIKeyboardWillHideNotification object:nil]; }
- (void)keyboardWillShow:(NSNotification *)notification {
NSDictionary* info = [notification userInfo];
NSNumber *duration = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [info objectForKey: UIKeyboardAnimationCurveUserInfoKey];
CGSize endKbSize = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size;
CGRect baseFrame = self.frame;
baseFrame.origin.y = CGRectGetMaxY(self.frame) - (endKbSize.height - 5);
[UIView animateWithDuration:duration.floatValue delay:0.0f options:curve.integerValue animations:^{
self.frame = baseFrame;
} completion:^(BOOL finished) {
self.frame = baseFrame;
}]; }
- (void)keyboardWillHide:(NSNotification *)notification {
NSDictionary* info = [notification userInfo];
NSNumber *duration = [info objectForKey:UIKeyboardAnimationDurationUserInfoKey];
NSNumber *curve = [info objectForKey: UIKeyboardAnimationCurveUserInfoKey];
[UIView animateWithDuration:duration.floatValue delay:0.0f options:curve.integerValue animations:^{
self.frame = _originalRect;
} completion:nil];
}
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