In my view contoller’s viewDidLoad function, I have this guy:
UIScrollView *scroller = [[UIScrollView alloc] initWithFrame:self.view.frame];
scroller.contentSize = self.view.frame.size; // Tried w/ and w/o this
scroller.showsVerticalScrollIndicator = YES; // Tried w/ and w/o this
for (int x = 0; x < 10; x++) {
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, x * 100, 100, 100)];
label.text = [NSString stringWithFormat:@"%i label", x];
[scroller addSubview:label];
}
[self.view addSubview:scroller];
It shows the first 8 labels OK, but the scroll view won’t… scroll. It is just cut off. Any idea why?
It won’t scroll because your
contentSizeis only set to the same size as the scrollview itself. ThecontentSizeis the size of the scrollable area that the scrollview acts as a window in to. You need to set it wide enough to actually see your labels.Based on your current code, your final label is in rect
{0, 900, 100, 100}, so yourcontentSizeneeds to be at leastCGSizeMake(100, 1000).