Created
July 5, 2012 09:50
-
-
Save anderssvendal/3052679 to your computer and use it in GitHub Desktop.
adjust for keyboard
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
- (void)adjustViewForTextField:(UITextField*)textField animated:(BOOL)animated { | |
CGRect frame = self.view.frame; | |
double screenSize = 480.0; | |
double topHeight = 20.0; | |
double keyboardSize = 216.0; | |
double keyboardPos = screenSize - keyboardSize; | |
double textFieldPos = topHeight + textField.frame.origin.y; | |
double textFieldHeight = textField.frame.size.height; | |
double textFieldBottom = textFieldPos + textFieldHeight; | |
if ((textFieldBottom + frame.origin.y) >= keyboardPos) { | |
frame.origin.y = (keyboardPos - textFieldPos) - textFieldHeight - 10.0; | |
if (animated) { | |
[UIView animateWithDuration:0.25 animations:^ { | |
self.view.frame = frame; | |
}]; | |
} | |
else { | |
self.view.frame = frame; | |
} | |
} | |
} | |
- (void)resetViewAfterEditingAnimated:(BOOL)animated { | |
CGRect frame = self.view.frame; | |
frame.origin.y = 0.0; | |
if (animated) { | |
[UIView animateWithDuration:0.25 animations:^ { | |
self.view.frame = frame; | |
}]; | |
} | |
else { | |
self.view.frame = frame; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment