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.
Here’s the 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 < 0.0)
{
heightFraction = 0.0;
}
else if (heightFraction > 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];
}
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.
2011-05-24 09:36:08.600 Baby Bloom[27794:207] Midline = 246 + 0.5 * 167
2011-05-24 09:36:08.601 Baby Bloom[27794:207] Midline: 329.5 Fraction: 22.3 / 409.6
2011-05-24 09:36:09.535 Baby Bloom[27794:207] Midline = 246 + 0.5 * 167
2011-05-24 09:36:09.536 Baby Bloom[27794:207] Midline: 329.5 Fraction: 22.3 / 409.6
2011-05-24 09:36:09.929 Baby Bloom[27794:207] Midline = 246 + 0.5 * 246
2011-05-24 09:36:09.930 Baby Bloom[27794:207] Midline: 369 Fraction: 61.8 / 409.6
2011-05-24 09:36:10.313 Baby Bloom[27794:207] Midline = 246 + 0.5 * 246
2011-05-24 09:36:10.314 Baby Bloom[27794:207] Midline: 369 Fraction: 61.8 / 409.6
2011-05-24 09:36:10.793 Baby Bloom[27794:207] Midline = 246 + 0.5 * 97
2011-05-24 09:36:10.794 Baby Bloom[27794:207] Midline: 294.5 Fraction: -12.7 / 409.6
2011-05-24 09:36:11.785 Baby Bloom[27794:207] Midline = 246 + 0.5 * 148
2011-05-24 09:36:11.786 Baby Bloom[27794:207] Midline: 320 Fraction: 12.8 / 409.6
Turns out that when in Landscape on the iPad it switches up the origin (x is now y and width is now height.
Here is the code to make it work: