输入期间移动文本字段

时间:2012-07-12 23:39:29

标签: iphone objective-c ios

我已经设置了一些文本字段和标签,看起来像一个简单的表单,必须得到一些用户输入。但问题是,只要选择了某些文本字段并且键盘滑出,就会覆盖文本字段,从而无法看到输入。我的问题是如何向上移动文本字段,以便在选中时保持在视图中。也许更有经验的人可以帮助我。

4 个答案:

答案 0 :(得分: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)

我希望有帮助

答案 1 :(得分:2)

这样做,你需要遵循下面的两个步骤:
- 将所有UITextViews放入UIScrollView
- 注册键盘通知,并在键盘出现时滑动UIScrollView 这两个步骤都在这里解释:"Moving Content That Is Located Under the Keyboard"

答案 2 :(得分: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];
}

答案 3 :(得分:1)

对于我的应用程序,我使用以下代码,假设keyboardHeight为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];
}

当用户在字段外点击时,您可以隐藏键盘。