我的代码有问题。我正在尝试UIScrollView
在编辑UITextField
时应移动键盘弹出键隐藏的。
我现在正在移动主机,因为我不知道如何在代码中“向上滚动”。因此,我做了一些代码,它工作正常,但是当我编辑一个UItextfield并切换到另一个UITextField
而不按“返回”按钮时,主视图会变得很远。
我对NSLog()
变量的大小,距离和textFieldRect.origin.y进行了设置,如下所示。当我将两个UITextField
放在同一位置(y原点)并且执行此特定的“切换”(不按回车键)时,我得到的数字是相同的,而我的代码在第一次UITextField
编辑时效果很好,但在第二次编辑时效果不佳。
看一下这个:
- (void)textFieldDidBeginEditing:(UITextField *)textField {
{
int size;
CGRect textFieldRect = [self.view.window convertRect:textField.bounds fromView:textField];
size = textFieldRect.origin.y + textFieldRect.size.height;
if (change == FALSE)
{
size = size - distance;
}
if (size < PORTRAIT_KEYBOARD_HEIGHT)
{
distance = 0;
}
else if (size > PORTRAIT_KEYBOARD_HEIGHT)
{
distance = size - PORTRAIT_KEYBOARD_HEIGHT + 5; // +5 px for more visibility
}
NSLog(@"origin %f", textFieldRect.origin.y);
NSLog(@"size %d", size);
NSLog(@"distance %d", distance);
CGRect viewFrame = self.view.frame;
viewFrame.origin.y -= distance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
change = FALSE;
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
change = TRUE;
CGRect viewFrame = self.view.frame;
viewFrame.origin.y += distance;
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationBeginsFromCurrentState:YES];
[UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];
[self.view setFrame:viewFrame];
[UIView commitAnimations];
}
有任何想法吗 ?