最近做项目开发,遇到了一个ios初学者都会遇到的问题,就是文本框被键盘遮盖了,而ios系统又不会自动给你换位置,这纠结我一段时间,先看一个百度出镜率比较高的方法
```
- (void)textFieldDidBeginEditing:(UITextField *)textField{
floatoffset =0.0f;
if(self.verificationCodeBox.textField == textField || self.phoneBox.textField == textField){
offset = -180.0f;
NSTimeIntervalanimationDuration =0.30f;
[UIView beginAnimations:@"ResizeForKeyBoard"context:nil];
[UIViewsetAnimationDuration:animationDuration];
floatwidth =self.view.frame.size.width;
floatheight =self.view.frame.size.height;
CGRectrect =CGRectMake(0.0f, offset , width, height);
self.view.frame= rect;
[UIView commitAnimations];
}
}
```
```
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textFieldresignFirstResponder];
return YES;
}
- (void)textFieldDidEndEditing:(UITextField *)textField{
floatoffset =0.0f;
if(self.verificationCodeBox.textField == textField || self.phoneBox.textField == textField){
NSTimeIntervalanimationDuration =0.30f;
[UIView beginAnimations:@"ResizeForKeyBoard"context:nil];
[UIViewsetAnimationDuration:animationDuration];
floatwidth =self.view.frame.size.width;
floatheight =self.view.frame.size.height;
CGRectrect =CGRectMake(0.0f, offset+64, width, height);
self.view.frame= rect;
[UIView commitAnimations];
}
}
```