You might be better off just placing the content on the screen in a scrollview, and then resizing the view when the keyboard is present. This is usually the approach that I take when I have an input form, and it is very easy to implement. To do this, you can follow the instructions in this answer:
fooobar.com/questions/538253 / ...
First, make sure you register your viewController for keyboard notifications (and remove observers when the view goes away):
- (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // register for keyboard notifications [[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 { [super viewWillDisappear:animated]; // unregister for keyboard notifications while not visible. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; }
Here is the bulk of the code. It is assumed that you have a view that covers the entire screen. If this is not the case, you need to configure CGRect in two ways below:
- (void)keyboardWillShow:(NSNotification *)note { NSDictionary *userInfo = note.userInfo; NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]; CGRect keyboardFrameEnd = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; keyboardFrameEnd = [self.view convertRect:keyboardFrameEnd fromView:nil]; [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{ self.contentView.frame = CGRectMake(0, 0, keyboardFrameEnd.size.width, keyboardFrameEnd.origin.y); } completion:nil]; } - (void)keyboardWillHide:(NSNotification *)note { NSDictionary *userInfo = note.userInfo; NSTimeInterval duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue]; UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue]; CGRect keyboardFrameEnd = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue]; keyboardFrameEnd = [self.view convertRect:keyboardFrameEnd fromView:nil]; [UIView animateWithDuration:duration delay:0 options:UIViewAnimationOptionBeginFromCurrentState | curve animations:^{ self.contentView.frame = CGRectMake(0, 0, keyboardFrameEnd.size.width, keyboardFrameEnd.origin.y); } completion:nil]; }
source share