当UIScrollView填满更多内容时,可以扩展它

时间:2011-07-26 09:20:57

标签: objective-c

好的,所以当用户登录我的应用程序时,我会加载一个scrollview。用户通过身份验证后,我需要以编程方式创建一个scrollview。为此,我有以下代码:

[scrollView removeFromSuperview];

        UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, applicationFrame.size.width, self.view.frame.size.height)];
        view.backgroundColor = [UIColor greenColor];


        //-- get the y-coordinate of the view
        CGFloat viewCenterY = self.view.center.y;
        //--calculate how much visible space is left
        CGFloat freeSpaceHeight = applicationFrame.size.height - keyboardBounds.size.height;
        //-- calculate how much the scrollview must scroll
        CGFloat scrollAmount = viewCenterY - freeSpaceHeight / 2.0;

        if(scrollAmount < 0) scrollAmount = 0;

        //set the new scrollsize contentview
        view.contentSize = CGSizeMake(applicationFrame.size.width,         applicationFrame.size.height + keyboardBounds.size.height);

        //scroll the scrollview
        [view setContentOffset:CGPointMake(0, scrollAmount) animated:YES];

            for(int v = 0; v < 15; v++){
                CGFloat yCoord = v * 100;
                UILabel *mijnLabel = [[UILabel alloc] initWithFrame:CGRectMake(50, yCoord, 200, 50)];

                mijnLabel.text = @"1337";
                mijnLabel.font = [UIFont fontWithName:@"Verdana" size:20];
                mijnLabel.textColor = [UIColor redColor];

                [view addSubview:mijnLabel];
                [mijnLabel release];
            }


            [self.view addSubview:view];

            [view release];


        }

如何根据我添加的标签数量扩展我的视图?我也想知道如何让我的滚动视图填满整个屏幕..我的滚动视图现在只在纵向模式下填满屏幕。

这是删除的登录屏幕:

 [scrollView removeFromSuperview];

1 个答案:

答案 0 :(得分:0)

您需要跟踪要添加到滚动视图中的标签数量。假设您的标签的高度为50,则取一个变量并总计所有标签的高度,并为滚动视图提供一个新框架。喜欢:

        view.frame = CGRectmake(0,0,applicationFrame.size.width,totalHeightOfLabel);
        [self.view addSubview:view];

        [view release];

希望这会给出一个想法...