Note that there are some explanatory texts on larger screens.

plurals
  1. POSliding UITextField on iPad when Keyboard Shows
    primarykey
    data
    text
    <p>I have code from Cocoa with Love that scrolls a UITextField when the keyboard comes up so that the keyboard does not cover the UITextField.</p> <p>Here's the code:</p> <pre><code>- (void)textFieldDidBeginEditing:(UITextField *)textField { CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField]; CGRect viewRect = [self.view.window convertRect:self.view.bounds fromView:self.view]; CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height; // Issue is that numerator isn't big enough for bottom 3rd of the screen CGFloat numerator = midline - viewRect.origin.y - MINIMUM_SCROLL_FRACTION * viewRect.size.height; CGFloat denominator = (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION) * viewRect.size.height; CGFloat heightFraction = numerator / denominator; NSLog(@"Midline: %g Fraction: %g / %g", midline, numerator, denominator); if (heightFraction &lt; 0.0) { heightFraction = 0.0; } else if (heightFraction &gt; 1.0) { heightFraction = 1.0; } UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; if (orientation == UIInterfaceOrientationPortrait || orientation == UIInterfaceOrientationPortraitUpsideDown) { animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction); } else { animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction); } CGRect viewFrame = self.view.frame; viewFrame.origin.y -= animatedDistance; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; [self.view setFrame:viewFrame]; [UIView commitAnimations]; } </code></pre> <p>For debugging purposes I output the midline, etc to see what was happening. This is from tabbing from top to bottom. You can see about the 6th one down the numerator becomes negative, because the UITExtField.size.height that was converted for it's parent view is a low number. I can't figure out why the number would be a negative. The height should up just like all the others do as you go down the view.</p> <p>2011-05-24 09:36:08.600 Baby Bloom[27794:207] Midline = 246 + 0.5 * 167</p> <p>2011-05-24 09:36:08.601 Baby Bloom[27794:207] Midline: 329.5 Fraction: 22.3 / 409.6</p> <p>2011-05-24 09:36:09.535 Baby Bloom[27794:207] Midline = 246 + 0.5 * 167</p> <p>2011-05-24 09:36:09.536 Baby Bloom[27794:207] Midline: 329.5 Fraction: 22.3 / 409.6</p> <p>2011-05-24 09:36:09.929 Baby Bloom[27794:207] Midline = 246 + 0.5 * 246</p> <p>2011-05-24 09:36:09.930 Baby Bloom[27794:207] Midline: 369 Fraction: 61.8 / 409.6</p> <p>2011-05-24 09:36:10.313 Baby Bloom[27794:207] Midline = 246 + 0.5 * 246</p> <p>2011-05-24 09:36:10.314 Baby Bloom[27794:207] Midline: 369 Fraction: 61.8 / 409.6</p> <p>2011-05-24 09:36:10.793 Baby Bloom[27794:207] Midline = 246 + 0.5 * 97</p> <p>2011-05-24 09:36:10.794 Baby Bloom[27794:207] Midline: 294.5 Fraction: -12.7 / 409.6</p> <p>2011-05-24 09:36:11.785 Baby Bloom[27794:207] Midline = 246 + 0.5 * 148</p> <p>2011-05-24 09:36:11.786 Baby Bloom[27794:207] Midline: 320 Fraction: 12.8 / 409.6</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. 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