Created
December 10, 2013 15:33
-
-
Save siannopollo/7892526 to your computer and use it in GitHub Desktop.
Starting with this tutorial (http://www.cocoawithlove.com/2008/10/sliding-uitextfields-around-to-avoid.html) I ended up with these methods to handle scrolling the view when the iOS keyboard is shown. This performs the minimum amount of scrolling needed to show the selected text field plus a 10 point padding. This was used on a view using autolay…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@interface DelegateController : UIViewController <UITextFieldDelegate> { | |
CGFloat keyboardAnimatedDistance; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@implementation DelegateController | |
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3; | |
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216; | |
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162; | |
- (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 viewHeight, bottomOfTextField, topOfKeyboard; | |
UIDeviceOrientation orientation = [UIDevice currentDevice].orientation; | |
if (orientation == UIDeviceOrientationPortrait || orientation == UIDeviceOrientationPortraitUpsideDown) { | |
viewHeight = viewRect.size.height; | |
bottomOfTextField = textFieldRect.origin.y + textFieldRect.size.height; | |
topOfKeyboard = viewHeight - PORTRAIT_KEYBOARD_HEIGHT - 10; // 10 point padding | |
} else { | |
viewHeight = viewRect.size.width; | |
bottomOfTextField = textFieldRect.origin.x + textFieldRect.size.width; | |
topOfKeyboard = viewHeight - LANDSCAPE_KEYBOARD_HEIGHT - 10; // 10 point padding | |
} | |
if (bottomOfTextField >= topOfKeyboard) keyboardAnimatedDistance = bottomOfTextField - topOfKeyboard; | |
else keyboardAnimatedDistance = 0.0f; | |
CGRect viewFrame = self.view.frame; | |
viewFrame.origin.y -= keyboardAnimatedDistance; | |
[UIView beginAnimations:nil context:NULL]; | |
[UIView setAnimationBeginsFromCurrentState:YES]; | |
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; | |
[self.view setFrame:viewFrame]; | |
[UIView commitAnimations]; | |
} | |
- (void)textFieldDidEndEditing:(UITextField *)textField { | |
CGRect viewFrame = self.view.frame; | |
viewFrame.origin.y += keyboardAnimatedDistance; | |
[UIView beginAnimations:nil context:NULL]; | |
[UIView setAnimationBeginsFromCurrentState:YES]; | |
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION]; | |
[self.view setFrame:viewFrame]; | |
[UIView commitAnimations]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment