下面方法来自 UITextFieldDelegate协议。
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
[UIView setAnimationDuration:animationDuration];
float with = self.view.frame.size.width;
float height = self.view.frame.size.height;
CGRect rect = CGRectMake(0.0f, 20.0f , with, height);
self.view.frame = rect;
[UIView commitAnimations];
[textField resignFirstResponder];
return YES;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
float offset = 0.0f;
if(self.field1 == textField || self.field3 == textField)
{
offset = - 30.0f;
}
else if(self.field2 == textField || self.field4 == textField)
{
offset = - 130.0f;
}
NSTimeInterval animationDuration = 0.30f;
[UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
[UIView setAnimationDuration:animationDuration];
float with = self.view.frame.size.width;
float height = self.view.frame.size.height;
CGRect rect = CGRectMake(0.0f, offset , with, height);
self.view.frame = rect;
[UIView commitAnimations];
}