The problem of getting UIScrollView height

I set the size of the UIScrollView in viewDidLoad: but when I try to get its height, I get 0 in the console

here is my code:

- (void)viewDidLoad {
    [scrollView setContentSize:CGSizeMake(320,500)];

    NSLog(@"scrollView: %@", scrollView);
    NSLog(@"scrollView.contentSize.height: %i", scrollView.contentSize.height);

    [super viewDidLoad];
}

and in the console log I get

scrollView: <UIScrollView: 0x4974110; frame = (0 0; 320 367); clipsToBounds = YES; autoresize = RM+TM; layer = <CALayer: 0x4974010>>
scrollView.contentSize.height: 0

Shouldn't scrollView.contentSize.height 367 be returned? In a related note, I pointed out that the height should be 500 through [scrollView setContentSize:], but this does not seem to apply?

+3
source share
1 answer

No. ContentSize - the size of the material inside the scrollview. Since there is nothing inside the scroll (i.e. you are not scrolling anything), the height is 0. The only way to get a valid contentSize is after adding a subview.

+1
source

Source: https://habr.com/ru/post/1741172/


All Articles