- (void) maintainVisibityOfControl:(UIControl *)control offset:(float)offset {
static const float deviceHeight = 480;
static const float keyboardHeight = 216;
static const float gap = 5; //gap between the top of keyboard and the control
//Find the controls absolute position in the 320*480 window - it could be nested in other views
CGPoint absolute = [control.superview convertPoint:control.frame.origin toView:nil];
//If it would be hidden behind the keyboard....
if (absolute.y > (keyboardHeight + gap)) {
//Shift the view
float shiftBy = (deviceHeight - absolute.y) - (deviceHeight - keyboardHeight - gap - offset);
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3f]; //this is speed of keyboard
CGAffineTransform slideTransform = CGAffineTransformMakeTranslation(0.0, shiftBy);
self.transform = slideTransform;
[UIView commitAnimations];
}
}
- (void) resetViewToIdentityTransform {
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:0.3f]; //this is speed of keyboard
CGAffineTransform slideTransform = CGAffineTransformIdentity;
self.transform = slideTransform;
[UIView commitAnimations];
}
- (void) textFieldDidBeginEditing:(UITextField *)textField {
[self.view maintainVisibityOfControl:textField offset:0.0f];
}
- (void)textFieldDidEndEditing:(UITextField *)textField {
if (textField == currentControl) {
//If the textfield is still the same one, we can reset the view animated
[self.view resetViewToIdentityTransform];
}else {
//However, if the currentControl has changed - that indicates the user has
//gone into another control - so don't reset view, otherwise animations jump around
}
}