Previously, I found the answer here , but I had to add code so that the View Viewer view.frame is configured when the user switches between keyboards (e.g. international or emoji) by pressing the globe key (
)
In YourViewController.h, save the height of the keyboard (only when it is visible) in the instance variable.
@interface YourViewController : UIViewController { CGFloat keyboardHeightIfShowing ; }
In YourViewController.m implement these methods.
- (void) keyboardWillShow:(NSNotification*)notification { [self moveTextViewForKeyboard:notification up:YES] ; } - (void) keyboardWillHide:(NSNotification*)notification { [self moveTextViewForKeyboard:notification up:NO] ; } - (void) moveTextViewForKeyboard:(NSNotification*)notification up:(BOOL)up { NSDictionary* userInfo = [notification userInfo] ; UIViewAnimationCurve animationCurve ; NSTimeInterval animationDuration ; CGRect keyboardEndFrame ; [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve] ; [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration] ; [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardEndFrame] ; [UIView beginAnimations:nil context:nil] ; [UIView setAnimationDuration:animationDuration] ; [UIView setAnimationCurve:animationCurve] ; CGRect keyboardFrame = [self.view convertRect:keyboardEndFrame toView:nil] ;
And finally, in YourViewController.m set keyboardWillShow and keyboardWillHide to call using UIKeyboardWillShow / Hide notifications.
- (void) viewWillAppear:(BOOL)animated { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil] ; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil] ; } - (void) viewWillDisappear:(BOOL)animated { [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil] ; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil] ; }
source share