Skip to content

Instantly share code, notes, and snippets.

@anderssvendal
Created July 5, 2012 09:50
Show Gist options
  • Save anderssvendal/3052679 to your computer and use it in GitHub Desktop.
Save anderssvendal/3052679 to your computer and use it in GitHub Desktop.
adjust for keyboard
- (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