Iphone 在输入过程中移动文本字段
我已经设置了一些文本字段和标签,看起来像一个简单的表单,需要用户输入。然而,我的问题是,每当选择一些文本字段并且键盘滑出时,文本字段就会被覆盖,从而无法看到输入。我的问题是如何向上移动文本字段,以便在选中时它们保持在视图中。也许有更有经验的人可以在这方面帮助我。要做到这一点,您需要遵循以下两个步骤:Iphone 在输入过程中移动文本字段,iphone,objective-c,ios,Iphone,Objective C,Ios,我已经设置了一些文本字段和标签,看起来像一个简单的表单,需要用户输入。然而,我的问题是,每当选择一些文本字段并且键盘滑出时,文本字段就会被覆盖,从而无法看到输入。我的问题是如何向上移动文本字段,以便在选中时它们保持在视图中。也许有更有经验的人可以在这方面帮助我。要做到这一点,您需要遵循以下两个步骤: -将所有的UITextView放入UIScrollView -注册键盘通知,并在键盘出现时滑动UIScrollView。 这里介绍了这两个步骤:我喜欢做的一个很好的解决方案是在文本字段开始编辑时使用
-将所有的
UITextView
放入UIScrollView
-注册键盘通知,并在键盘出现时滑动
UIScrollView
。这里介绍了这两个步骤:我喜欢做的一个很好的解决方案是在文本字段开始编辑时使用以下委托函数进行监听:
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
//keep a member variable to store where the textField started
_yPositionStore = textField.frame.origin.y;
//If we begin editing on the text field we need to move it up to make sure we can still
//see it when the keyboard is visible.
//
//I am adding an animation to make this look better
[UIView beginAnimations:@"Animate Text Field Up" context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationBeginsFromCurrentState:YES];
commentTextField.frame = CGRectMake(commentTextField.frame.origin.x,
160 , //this is just a number to put it above the keyboard
commentTextField.frame.size.width,
commentTextField.frame.size.height);
[UIView commitAnimations];
}
然后在此回调中,您可以将其返回到原始位置:
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[UIView beginAnimations:@"Animate Text Field Up" context:nil];
[UIView setAnimationDuration:.3];
[UIView setAnimationBeginsFromCurrentState:YES];
commentTextField.frame = CGRectMake(commentTextField.frame.origin.x,
_yPositionStore ,
commentTextField.frame.size.width,
commentTextField.frame.size.height);
[UIView commitAnimations];
}
}
您需要将_yPositionStore声明为CGFloat的成员变量,并将您的textFields委托设置为拥有它的视图控制器(使用界面生成器并将委托拖动到文件所有者或设置yourTextField.delegate=self)
我希望这有助于您可以调用UITextfield的委托方法,并根据需要对其进行自定义
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField: textField up: YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
[self animateTextField: textField up: NO];
}
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
const int movementDistance = 110;
const float movementDuration = 0.3f;
int movement = (up ? movementDistance : -movementDistance);
[UIView beginAnimations: @"aimation" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
yourView.frame= CGRectOffset(yourView.frame,0, movement);
[UIView commitAnimations];
}
对于我的应用程序,我使用以下代码,假设纵向模式的键盘高度为216,横向模式为162:
#pragma mark - textField delegate
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
// keyboard is visible, move views
CGRect myScreenRect = [[UIScreen mainScreen] bounds];
int keyboardHeight = 216;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.35];
int needToMove = 0;
CGRect frame = self.view.frame;
if (textField.frame.origin.y + textField.frame.size.height + self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height > (myScreenRect.size.height - keyboardHeight)) {
needToMove = (textField.frame.origin.y + textField.frame.size.height + self.navigationController.navigationBar.frame.size.height + [UIApplication sharedApplication].statusBarFrame.size.height) - (myScreenRect.size.height - keyboardHeight);
}
frame.origin.y = -needToMove;
[self.view setFrame:frame];
[UIView commitAnimations];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
// resign first responder, hide keyboard, move views
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDuration:0.35];
CGRect frame = self.view.frame;
frame.origin.y = 0;
[self.view setFrame:frame];
[UIView commitAnimations];
}
此公式考虑了状态栏、导航栏和显示大小。当然,不要忘记为字段设置委托
以及:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
}
当用户点击字段外时,您可以隐藏键盘。很酷,感谢您向我显示此链接。这正是我想要的。这实际上帮了我不少忙。我忘了设置文本字段。我现在唯一需要解决的是,一旦用户按下键盘上的“完成”按钮,就关闭键盘。有什么建议吗?当应用程序从后台恢复时,它的视图框将是错误的