Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The following code does what you want and works with any device and any layout. The code is courtesy of the <a href="http://sensiblecocoa.com">Sensible TableView framework</a> (with permission to copy and use).</p> <pre><code>- (void)keyboardWillShow:(NSNotification *)aNotification { if(keyboardShown) return; keyboardShown = YES; // Get the keyboard size UIScrollView *tableView; if([self.tableView.superview isKindOfClass:[UIScrollView class]]) tableView = (UIScrollView *)self.tableView.superview; else tableView = self.tableView; NSDictionary *userInfo = [aNotification userInfo]; NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; CGRect keyboardRect = [tableView.superview convertRect:[aValue CGRectValue] fromView:nil]; // Get the keyboard's animation details NSTimeInterval animationDuration; [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&amp;animationDuration]; UIViewAnimationCurve animationCurve; [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&amp;animationCurve]; // Determine how much overlap exists between tableView and the keyboard CGRect tableFrame = tableView.frame; CGFloat tableLowerYCoord = tableFrame.origin.y + tableFrame.size.height; keyboardOverlap = tableLowerYCoord - keyboardRect.origin.y; if(self.inputAccessoryView &amp;&amp; keyboardOverlap&gt;0) { CGFloat accessoryHeight = self.inputAccessoryView.frame.size.height; keyboardOverlap -= accessoryHeight; tableView.contentInset = UIEdgeInsetsMake(0, 0, accessoryHeight, 0); tableView.scrollIndicatorInsets = UIEdgeInsetsMake(0, 0, accessoryHeight, 0); } if(keyboardOverlap &lt; 0) keyboardOverlap = 0; if(keyboardOverlap != 0) { tableFrame.size.height -= keyboardOverlap; NSTimeInterval delay = 0; if(keyboardRect.size.height) { delay = (1 - keyboardOverlap/keyboardRect.size.height)*animationDuration; animationDuration = animationDuration * keyboardOverlap/keyboardRect.size.height; } [UIView animateWithDuration:animationDuration delay:delay options:UIViewAnimationOptionBeginFromCurrentState animations:^{ tableView.frame = tableFrame; } completion:^(BOOL finished){ [self tableAnimationEnded:nil finished:nil contextInfo:nil]; }]; } } - (void)keyboardWillHide:(NSNotification *)aNotification { if(!keyboardShown) return; keyboardShown = NO; UIScrollView *tableView; if([self.tableView.superview isKindOfClass:[UIScrollView class]]) tableView = (UIScrollView *)self.tableView.superview; else tableView = self.tableView; if(self.inputAccessoryView) { tableView.contentInset = UIEdgeInsetsZero; tableView.scrollIndicatorInsets = UIEdgeInsetsZero; } if(keyboardOverlap == 0) return; // Get the size &amp; animation details of the keyboard NSDictionary *userInfo = [aNotification userInfo]; NSValue *aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey]; CGRect keyboardRect = [tableView.superview convertRect:[aValue CGRectValue] fromView:nil]; NSTimeInterval animationDuration; [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&amp;animationDuration]; UIViewAnimationCurve animationCurve; [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&amp;animationCurve]; CGRect tableFrame = tableView.frame; tableFrame.size.height += keyboardOverlap; if(keyboardRect.size.height) animationDuration = animationDuration * keyboardOverlap/keyboardRect.size.height; [UIView animateWithDuration:animationDuration delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{ tableView.frame = tableFrame; } completion:nil]; } - (void) tableAnimationEnded:(NSString*)animationID finished:(NSNumber *)finished contextInfo:(void *)context { // Scroll to the active cell if(self.activeCellIndexPath) { [self.tableView scrollToRowAtIndexPath:self.activeCellIndexPath atScrollPosition:UITableViewScrollPositionNone animated:YES]; [self.tableView selectRowAtIndexPath:self.activeCellIndexPath animated:NO scrollPosition:UITableViewScrollPositionNone]; } } </code></pre> <p>Notes:</p> <p>a. The above two methods have been added to the notification center using the following code:</p> <pre><code>[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil]; </code></pre> <p>b. The ivars used above has been declared like this:</p> <pre><code>BOOL keyboardShown; CGFloat keyboardOverlap; </code></pre> <p>c. 'self.activeCellIndexPath' is always set to the indexPath of the cell owning the currently active UITextField/UITextView.</p> <p>Enjoy! :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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