Not so curious case of AutoLayout: iPhone

We found something new; I mean it happened to me first time. I still haven’t got the full explanation on it. My friend wrote a code to change the content size and frame of UIScrollView at viewdidLoad method. Nothing strange with that. We have done that many times on our projects. Our UIScrollView was supposed to be scrolled. It had worked, why wasn’t it supposed to work? We deduced to the source of cause but no explanation. It was happening only if we used “AutoLayout” else it seemed to work normal. We had to use “AutoLayout” for our reasons like we want to have universal layout compatibility with iPad and all sizes with more ease and less work.

-(void)viewDidLoad
{
    CGRect rect = self.mContainerScrollView.frame;
    self.mContainerScrollView.contentSize = CGSizeMake(320, rect.size.height);
    rect.size.height = self.view.frame.size.height - rect.origin.y;
    
    self.mContainerScrollView.frame = rect;
}

We moved the code to viewDidLayoutSubviews; yeah it’s working. We could scroll. I am looking for this explanation. I would really thankful and glad if anyone gives me the detail insights on this “AutoLayout” scenario.

-(void)viewDidLayoutSubviews
{
    CGRect rect = self.mContainerScrollView.frame;
    self.mContainerScrollView.contentSize = CGSizeMake(320, rect.size.height);
    rect.size.height = self.view.frame.size.height - rect.origin.y;
    
    self.mContainerScrollView.frame = rect;
}

Be the first to leave a comment. Don’t be shy.

Join the Discussion

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>