我已经设置了一些文本字段和标签,使其看起来像一个简单的表单,必须获得一些用户输入。My problem is however that whenever some of the text fields are selected and the keyboard slides out, the text fields are covered making it impossible to see the input. 我的问题是如何向上移动文本字段,以便它们在选择时保持在视图中。也许有经验的人可以帮助我。
问问题
2331 次
4 回答
4
我喜欢做的一个很好的解决方案是在文本字段开始使用以下委托函数进行编辑时收听:
- (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)
我希望这会有所帮助
于 2012-07-12T23:50:42.420 回答
2
这样做你需要遵循接下来的 2 个步骤:
- 将所有内容UITextViews
放入UIScrollView
- 注册键盘通知并UIScrollView
在键盘出现时滑动你的
两个步骤在这里解释:“移动位于键盘下方的内容”
于 2012-07-12T23:50:25.137 回答
1
您可以调用 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];
}
于 2012-12-03T09:07:32.093 回答
1
对于我的应用程序,我使用以下代码,假设键盘高度为 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];
}
当用户在字段外点击时,您可以隐藏键盘。
于 2013-09-14T14:34:20.017 回答