Automatically adjust UIScrollView Height

Go To StackoverFlow.com

0

How would I go about dynamically adjusting the height of a UIScrollView? Basically I have a bunch of UILabels that are created (the exact number of UILabels is random) and the UIScrollView is to adjust its height automatically to accommodate the UILables. This is what I have thus far in the viewDidLoad.

- (void)viewDidLoad
{
[scroller setScrollEnabled:YES];
[scroller setContentSize:CGSizeMake(320
                                    , 1500)];
scroller.indicatorStyle = UIScrollViewIndicatorStyleWhite;
{ 

And this is the action that creates additional UILavels

-(IBAction)scheduale{
int i;
for(i=0; i<[self retrieveTime] ; i++){
    //Add time label
    UILabel *timeLabel = [[UILabel alloc] init];
    timeLabel.frame = CGRectMake(10, (i+1) * 21, 31, 20);
    timeLabel.textColor = [UIColor whiteColor];
    timeLabel.backgroundColor = [UIColor colorWithRed:76.0/225.0 green:76.0/225.0 blue:76.0/225.0 alpha:1.0];
    NSString *labelString;
    labelString = [[NSNumber numberWithInt:i] stringValue];
    timeLabel.text = labelString;
    timeLabel.textAlignment = UITextAlignmentCenter;
    //theLabel.tag = (i+1) * 100;
    [scroller addSubview:timeLabel];
}
2012-04-05 02:12
by SNV7


2

You need to hold onto a variable to keep track of how many labels you are adding and their height to set the content size when you are done creating labels. See adjusted code below:

-(IBAction)scheduale{

 int i; 
 int contentSize = 0;

 for(i=0; i<[self retrieveTime] ; i++){

     UILabel *timeLabel = [[UILabel alloc] init];
     timeLabel.frame = CGRectMake(10, (i+1) * 21, 31, 20);
     contentSize += 20;
     timeLabel.textColor = [UIColor whiteColor];
     timeLabel.backgroundColor = [UIColor colorWithRed:76.0/225.0 green:76.0/225.0 blue:76.0/225.0 alpha:1.0];
     NSString *labelString;
     labelString = [[NSNumber numberWithInt:i] stringValue];
     timeLabel.text = labelString;
     timeLabel.textAlignment = UITextAlignmentCenter;
     //theLabel.tag = (i+1) * 100;
     [scroller addSubview:timeLabel]; }

 [scroller setContentSize:CGSizeMake(320, contentSize)];
2012-04-05 02:26
by Kyle Richter
please double check that contentSize advances properly - danh 2012-04-05 03:03


0

I agree with @Kyle, but the content size will advance with the positions of the labels, ending with the last position + last height. So his idea needs a little tweak there.

Also, just to confirm: @world peace - do you want to change the scroller's frame, or just it's content size? @Kyle is correct that you must at least change the content size.

Here's a little cleanup on that code:

#define kButtonWidth   31.0
#define kButtonHeight  20.0
#define kMargin         1.0

-(IBAction)scheduale {

    int i;
    CGFloat contentPositionY = 0.0;

    UIColor *labelColor = [UIColor colorWithRed:76.0/225.0 green:76.0/225.0 blue:76.0/225.0 alpha:1.0];

    for(i=0; i<[self retrieveTime] ; i++){
        //Add time label
        UILabel *timeLabel = [[UILabel alloc] init];

        timeLabel.frame = CGRectMake(0, contentPositionY, kButtonWidth, kButtonHeight);
        contentPositionY += kButtonHeight + kMargin;

        timeLabel.textColor = [UIColor whiteColor];
        timeLabel.backgroundColor = labelColor;

        timeLabel.text = [NSString stringWithFormat:@"@d", i];

        timeLabel.textAlignment = UITextAlignmentCenter;
        //theLabel.tag = (i+1) * 100;
        [scroller addSubview:timeLabel];
    }

    // now size the content
    scroller.contentSize = CGSizeMake(kButtonWidth, contentPositionY + kButtonHeight);

    // and to get the margins you built into the loop calculation
    scroller.contentInset = UIEdgeInsetsMake(21, 10, 0, 0);   // your code implied these values
}
2012-04-05 03:02
by danh
Ads