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];
}
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: