通用方法解决UITextFiled输入的时候,键盘遮挡问题
- (void)moveView:(UITextField *)textField leaveView:(BOOL)leave { UIView *accessoryView = textField.inputAccessoryView; UIView *inputview = textField.inputView; int textFieldY = 0; int accessoryY = 0; if (accessoryView && inputview) { CGRect accessoryRect = accessoryView.frame; CGRect inputViewRect = inputview.frame; accessoryY = 480 - (accessoryRect.size.height + inputViewRect.size.height); } else if (accessoryView) { CGRect accessoryRect = accessoryView.frame; accessoryY = 480 - (accessoryRect.size.height + 216); } else if (inputview) { CGRect inputViewRect = inputview.frame; accessoryY = 480 -inputViewRect.size.height; } else { accessoryY = 264; //480 - 216; } CGRect textFieldRect = textField.frame; textFieldY = textFieldRect.origin.y + textFieldRect.size.height + 20; int offsetY = textFieldY - accessoryY; if (!leave && offsetY > 0) { int y_offset = -5; y_offset += -offsetY; CGRect viewFrame = self.view.frame; viewFrame.origin.y += y_offset; [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:0.3]; [self.view setFrame:viewFrame]; [UIView commitAnimations]; } else { CGRect viewFrame = CGRectMake(0, 20, 320, 460); [UIView beginAnimations:nil context:NULL]; [UIView setAnimationBeginsFromCurrentState:YES]; [UIView setAnimationDuration:0.3]; [self.view setFrame:viewFrame]; [UIView commitAnimations]; } }
- (void)textFieldDidBeginEditing:(UITextField *)textField { [self moveView:textField leaveView:NO]; } - (void)textFieldDidEndEditing:(UITextField *)textField; { [self moveView:textField leaveView:YES]; }