我有一个应用程序,在 Interface Builder 中,我设置了一个 UIView
,它在 View 底部附近有一个文本字段。当我运行该应用程序并尝试在该字段中输入文本时,键盘会滑到该字段上方,因此在我再次隐藏键盘之前我看不到我正在输入的内容。
有没有其他人遇到过这个问题并找到了一个既不使父 View 可滚动也不将文本字段移到屏幕更远的位置来解决它的好方法?
最佳答案
通常的解决方案是使用动画将字段(及其上方的所有内容)向上滑动,然后在完成后向下滑动。您可能需要将文本字段和其他一些项目放入另一个 View 并将 View 作为一个单元滑动。 (我称这些东西为“板 block ”,就像“构造板 block ”一样,但那只是我)。但如果您不需要花哨的话,这里是一般的想法。
- (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 = 80; // tweak as needed
const float movementDuration = 0.3f; // tweak as needed
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: @"anim" context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
[UIView commitAnimations];
}
关于ios - iPhone 键盘盖 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1247113/