Endive au Jambon
Endive au Jambon

Reputation: 471

UIScrollView not updating after calling setContentSize in viewDidLoad, but does when called from a button

I have a ViewController with a UIScrollView that contains a View that contains variable-sized content.

- ViewController
    -ScrollView
        -View
            -label1
            -label2
            - ...

When the controller's viewDidLoad is called, I set the UIScrollView's content size to the appropriate height, but the view doesn't scroll. If I call the same line from an action on a button, it does update the contentSize and scrolls correctly. I am using autolayout. Any idea why and how to fix it?

The view won't scroll:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [_scrollView setContentSize:CGSizeMake(100,500)];
}

The view will scroll correctly:

- (IBAction)test:(id)sender 
{
    [_scrollView setContentSize:CGSizeMake(100,500)];
}

Upvotes: 2

Views: 291

Answers (3)

user3559963
user3559963

Reputation: 9

  • ViewController -ScrollView -View -label1 -label2

First ScrollView add new contraints and next label1 and label2 add contraints then final View view can add contraints.

Upvotes: 0

Pandey_Laxman
Pandey_Laxman

Reputation: 3908

ViewDidAppear also Guarantees that your view has been loaded and initialized But viewdidAppear fires after viewDidLayoutSubviews. if you do large coding in ViewDidLoad Realted to UI your ViewController delayed in display

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:NO];
    [_scrollView setContentSize:CGSizeMake(100,500)];
}

Upvotes: 1

Rukshan
Rukshan

Reputation: 8066

Don't set content size in viewDidLoad. Do it in viewDidAppear

Upvotes: 1

Related Questions