iPhone - 键盘隐藏TextField

int*_*ntl 22 iphone keyboard uitextfield

我正在使用UITextField来接收用户输入.但是,由于我的文本字段朝向笔尖的中间/底部,因此当键盘弹出时会隐藏它.有没有什么方法可以将它与键盘一起滑动,以便在选择时它位于键盘的顶部?此外,由于我也在使用数字键盘,是否有一种简单的方法可以在某处包含完成按钮?

谢谢您的帮助.

Val*_*tel 26

我已经为这个问题做了一个简单的应用程序.它会自动检查Textfield的位置,如果键盘隐藏它,它会根据需要自动向上移动.


- (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
{
    int animatedDistance;
    int moveUpValue = textField.frame.origin.y+ textField.frame.size.height;
    UIInterfaceOrientation orientation =
    [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationPortrait ||
        orientation == UIInterfaceOrientationPortraitUpsideDown)
    {

        animatedDistance = 216-(460-moveUpValue-5);
    }
    else
    {
        animatedDistance = 162-(320-moveUpValue-5);
    }

    if(animatedDistance>0)
    {
        const int movementDistance = animatedDistance;
        const float movementDuration = 0.3f; 
        int movement = (up ? -movementDistance : movementDistance);
        [UIView beginAnimations: nil context: nil];
        [UIView setAnimationBeginsFromCurrentState: YES];
        [UIView setAnimationDuration: movementDuration];
        self.view.frame = CGRectOffset(self.view.frame, 0, movement);       
        [UIView commitAnimations];
    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField {

    [textField resignFirstResponder];
    return YES;
}


ger*_*ry3 17

要在键盘出现时滚动,我喜欢Cocoa With Love的这个教程.

要关闭数字键盘,您可以在键盘上放置一个自定义的"完成"按钮,或在屏幕的其余部分上设置一个隐形按钮.我用代码完成了后者,但本教程使用Interface Builder.

  • 第三个链接坏了:( (2认同)