Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You need to add notifications to catch the keyboard status. In the notification you can capture the keyboard size and move the view according to the keyboard size.</p> <pre><code>#define kKeyboardAnimationDuration 0.3 @interface YourViewController:UIViewController { BOOL keyboardIsShown; } </code></pre> <p>And then in your implementation</p> <pre><code>- (void)viewDidLoad { // register for keyboard notifications [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:self.view.window]; // register for keyboard notifications [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:self.view.window]; keyboardIsShown = NO; } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; // unregister for keyboard notifications while not visible. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil]; // unregister for keyboard notifications while not visible. [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil]; } #pragma mark - Keyboard Events - (void)keyboardWillShow:(NSNotification *)n { if (keyboardIsShown) { return; } NSDictionary* userInfo = [n userInfo]; CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; CGRect viewFrame = self.view.frame; viewFrame.origin.y-= (keyboardSize.height); [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:kKeyboardAnimationDuration]; [self.view setFrame:viewFrame]; [UIView commitAnimations]; keyboardIsShown = YES; } - (void)keyboardWillHide:(NSNotification *)n { NSDictionary* userInfo = [n userInfo]; CGSize keyboardSize = [[userInfo objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size; CGRect viewFrame = self.view.frame; viewFrame.origin.y+= (keyboardSize.height); [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:kKeyboardAnimationDuration]; [self.view setFrame:viewFrame]; [UIView commitAnimations]; keyboardIsShown = NO; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload