我的代码有问题。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];
}
有任何想法吗 ?