我在 ipad 上有一个表格,最后有很多文本字段和一个按钮。当它处于活动状态时,键盘下方有一些字段。为了使键盘后面的隐藏 texfield 可见,我正在使用以下代码。
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[self animateTextField:textField up:YES];
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
_scrollView.frame=CGRectMake(0, 0, 1024, 655);
[self animateTextField:textField up:NO];
}
- (void) animateTextField: (UITextField*) textField up: (BOOL) up
{
CGPoint temp = [textField.superview convertPoint:textField.frame.origin toView:nil];
UIInterfaceOrientation orientation =
[[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait)
{
// NSLog(@"portrait");
if(up)
{
int moveUpValue = temp.y+textField.frame.size.height;
animatedDis = 264-(1024-moveUpValue-5);
}
}
else if(orientation == UIInterfaceOrientationPortraitUpsideDown)
{
if(up)
{
int moveUpValue = 1004-temp.y+textField.frame.size.height;
animatedDis = 264-(1004-moveUpValue-5);
}
}
else if(orientation == UIInterfaceOrientationLandscapeLeft)
{
if(up)
{
int moveUpValue = temp.x+textField.frame.size.height;
animatedDis = 352-(768-moveUpValue-5);
}
}
else
{
if(up)
{
int moveUpValue = 768-temp.x+textField.frame.size.height;
animatedDis = 352-(768-moveUpValue-5);
_scrollView.frame = CGRectMake(0, 0, 1024, 655-240);
}
}
if(animatedDis>0)
{
const int movementDistance = animatedDis;
const float movementDuration = 0.3f;
int movement = (up ? -movementDistance : movementDistance);
[UIView beginAnimations: nil context: nil];
[UIView setAnimationBeginsFromCurrentState: YES];
[UIView setAnimationDuration: movementDuration];
if (orientation == UIInterfaceOrientationPortrait)
{
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
}
else if(orientation == UIInterfaceOrientationPortraitUpsideDown)
{
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
}
else if(orientation == UIInterfaceOrientationLandscapeLeft)
{
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
}
else
{
self.view.frame = CGRectOffset(self.view.frame, 0, movement);
}
[UIView commitAnimations];
}
}
我也在使用滚动视图。我的问题是,当键盘处于活动状态并且我按下按钮时,它会将我带到下一个屏幕。现在,如果我向后导航,则键盘处于活动状态并设置了先前的动画。现在,如果我隐藏键盘,整个视图会向下滚动,并在顶部留下黑色部分。如何处理这种情况?