title: Getting size of styled UIWebView content - Not quite working
body: <p>This seems like a common problem, but I'm unable find an adequate solution. I'm trying to re-size a bunch of UIWebViews to fit their content, but it doesn't seem to work properly. The best solution I've found so far is <a href="https://stackoverflow.com/a/3937599/304786">this answer</a>, but it only appears to work occasionally in my solution. The idea is to re-size the UIWebView in the <code>webViewDidFinishLoad:</code> delegate method. However, this still doesn't get the right size. If I trigger the update at some undefined time <em>after</em> <code>webViewDidFinishLoad:</code>, the content does get sized correctly.</p>
  
  <p>I will describe my situation below, and I'll paste relevant source on <a href="https://gist.github.com/3173830" rel="nofollow noreferrer">Gist</a>. </p>
  
  <p><strong>Problem Description</strong></p>
  
  <p>I have a situation where I'm getting data from a JSON API and displaying the data in webviews. The API returns an array of objects, and each object has a "content" field with HTML-formatted text. The designers have given a structure which involved a (mostly) hidden view that you swipe up to reveal, which contains a scroll view of all the content, formatted in a very particular way.</p>
  
  <p>To implement this, I have three view controllers: The main view controller, the secondary view controller which you swipe up on the main view controller to reveal, and a tertiary view controller that displays one item of html-formatted content from the JSON API results.<br>
  The secondary view controller contains a UIScrollView and an array of tertiary view controllers. Each tertiary view controller contains a UIWebView to display the HTML-formatted content, and labels for displaying other attributes of the JSON object. Because the tertiary views will be displayed inside a UIScrollView in the secondary view, the tertiary views' UIWebView needs to be re-sized to fit its content and have user interaction disabled in order to prevent a confusing scroll-view-in-scroll-view scenario.</p>
  
  <p>The process to get all this to work is as follows:  </p>
  
  <ol>
  <li>First, on the Primary controllers ViewDidLoad, load up and display the secondary view
  and an activity indicator so the UI does not appear to freeze</li>
  <li>Next, load the data from the JSON API</li>
  <li>When the data from the API has loaded (notified via a delegate), construct tertiary
  view controllers, set their attributes from the loaded data and add them to the secondary
  controller's list.</li>
  <li>Once that is finished, call the Secondary controller's setup method. This method loops
  through the list of tertiary controllers and calls their setup methods in turn   </li>
  <li>Each tertiary controller's setup method sets the text for the labels and sets the
  content for the UIWebView using <code>loadHTMLString:</code> with a pre-defined html file's content,
  with the content of the JSON object substituted in.</li>
  <li><strong>On the tertiary controller's <code>webViewDidFinishLoad:</code> delegate method, using the
  method at <a href="https://stackoverflow.com/a/3937599/304786">this answer</a>, re-size the web view based on the content size.</strong></li>
  <li>Re-size the tertiary controller's view to fit the web view and position any labels that
  go underneath the web view</li>
  <li>The tertiary controller then calls a delegate method to notify the secondary controller
  that it has finished loading. The secondary controller keeps tabs on which of its
  tertiary controllers have finished loading.</li>
  <li>When all tertiary controllers are finished loading, the secondary controller loops
  through its list and adds the tertiary views to it's UIScrollView's content, adjusting
  frames and the content height as it goes.</li>
  </ol>