title: How to scroll a UIScrollView on load when using auto layout
body: <p>I have a UIScrollView that is set up in the <code>viewDidLoad</code> method of a <code>UIViewController</code> (I'll call it a <code>ScrollViewController</code>). The scroll view contains pages of horizontal content (similar to the native weather app).</p>
  
  <p>When displaying the scrollview, it should be possible to choose which page it starts on. My pattern is this:</p>
  
  <ul>
  <li>init a <code>ScrollViewController</code>. Nothing much happens in here. A <code>currentPage</code> property is defaulted to 0.</li>
  <li>set the <code>scrollViewController.currentPage</code> to the desired page number.</li>
  <li>in <code>viewDidLoad</code> of <code>ScrollViewController</code>, read <code>self.currentPage</code> and use <code>scrollToRect</code> or <code>setContentOffset</code> to scroll accordingly.</li>
  </ul>
  
  <p>The scrolling implementation seems fine, since I am using the same code elsewhere to jump to certain pages. But on first load, nothing happens (that is, the scroll view is not scrolled to the desired page).</p>
  
  <p>I <strong>think</strong> I have found the reason - it seems that the <code>contentSize</code> of the scroll view (which is derived by autolayout remember) is 0 during <code>viewDidLoad</code>, and this seems that this prevents scrolling. It is also zero during viewWillAppear. Only in <code>viewDidAppear</code> does the scroll work, which of course makes for an odd user experience.</p>
  
  <p>How can I resolve this? Is it wise to somehow force a layout in <code>viewDidLoad</code>? Or some other approach?</p>