为包含少量文本字段的示例表单添加滚动视图(iphone)

时间:2013-02-25 01:27:39

标签: iphone uiscrollview

我有一个定义如下的注册表单,我喜欢在滚动视图中使用此表单,以便如果用户选择底部的文本字段,则滚动。

问题是,注册表单不会覆盖整个屏幕,在后台我仍然有主视图,我希望文本字段在表单内滚动,而不更改窗体窗口的位置(我不希望窗体视图出现并覆盖背景)。

    - (UIView *)Form
{
    if (!_Form) {

        CGRect frame = CGRectMake(0.0, Height, Width, 310.0);
        UIView *container = [[UIView alloc] initWithFrame:frame];

        CGFloat y = 15.0;
        frame = CGRectMake(15.0, y, width, height);
        UITextField *field = [[UITextField alloc] initWithFrame:frame];
        [
        field.placeholder = @"text1*";


        CGFloat spacing = 8.0;
        y = frame.origin.y + kDefaultTextFieldHeight + spacing;
        frame = CGRectMake(15.0, y, kDeviceWidth - 2*15.0, kDefaultTextFieldHeight);
        field = [[UITextField alloc] initWithFrame:frame];

        field.placeholder = @"Password*";


        frame.size.height = 200.0;

        y = frame.origin.y + kDefaultTextFieldHeight + spacing;
        frame = CGRectMake(15.0, y, kDeviceWidth - 2*15.0, kDefaultTextFieldHeight);
        field = [[UITextField alloc] initWithFrame:frame];

        field.placeholder = @"Confirm Password*";



        y = frame.origin.y + kDefaultTextFieldHeight + 16.0;
        CGFloat w = (kDeviceWidth - 2 * 15.0) / 2;
        frame = CGRectMake(15.0, y, w - 2.0, height);
        field = [[UITextField alloc] initWithFrame:frame];

        field.placeholder = @"First Name*";


        frame = CGRectMake(15.0 + w + 2.0, y, w - 2.0, height);
        field = [[UITextField alloc] initWithFrame:frame];

        field.placeholder = @"Last Name*";
        [container addSubview:field];

        y = frame.origin.y + kDefaultTextFieldHeight + spacing;
        frame = CGRectMake(15.0, y, w, kDefaultTextFieldHeight);
        field = [[UITextField alloc] initWithFrame:frame];

        field.placeholder = @"Birthday";



        y = frame.origin.y + kDefaultTextFieldHeight + 20.0;
        frame = CGRectMake((container.frame.size.width - 192.0) / 2, y, 192.0, 34.0);



        y = frame.origin.y + kDefaultTextFieldHeight + 8.0;
        frame = CGRectMake((container.frame.size.width - 192.0) / 2, y, 192.0, 34.0);



        _Form = container;
    }
    return _Form;
}

谢谢!

1 个答案:

答案 0 :(得分:0)

根据您的要求尝试并更改以下代码:

static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLL_FRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 162;


- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect textFieldRect =
    [self.view.window convertRect:textField.bounds fromView:textField];
    CGRect viewRect =
    [self.view.window convertRect:self.view.bounds fromView:self.view];

    CGFloat midline = textFieldRect.origin.y + 0.5 * textFieldRect.size.height;
    CGFloat numerator =
    midline - viewRect.origin.y
    - MINIMUM_SCROLL_FRACTION * viewRect.size.height;
    CGFloat denominator =
    (MAXIMUM_SCROLL_FRACTION - MINIMUM_SCROLL_FRACTION)
    * viewRect.size.height;
    CGFloat heightFraction = numerator / denominator;

    if (heightFraction < 0.0)
    {
        heightFraction = 0.0;
    }
    else if (heightFraction > 1.0)
    {
        heightFraction = 1.0;
    }

    UIInterfaceOrientation orientation =
    [[UIApplication sharedApplication] statusBarOrientation];
    if (orientation == UIInterfaceOrientationPortrait ||
        orientation == UIInterfaceOrientationPortraitUpsideDown)
    {
        animatedDistance = floor(PORTRAIT_KEYBOARD_HEIGHT * heightFraction);
    }
    else
    {
        animatedDistance = floor(LANDSCAPE_KEYBOARD_HEIGHT * heightFraction);
    }

    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y -= animatedDistance;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [self.view setFrame:viewFrame];

    [UIView commitAnimations];


}


- (void)textFieldDidEndEditing:(UITextField *)textField
{
    CGRect viewFrame = self.view.frame;
    viewFrame.origin.y += animatedDistance;

    [UIView beginAnimations:nil context:NULL];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationDuration:KEYBOARD_ANIMATION_DURATION];

    [self.view setFrame:viewFrame];

    [UIView commitAnimations];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}