Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I have another solution that works great.</p> <p>On one hand, Ortwin's approach &amp; solution works only with iOS 6.0 and later, but fails to work correctly on iOS 5.0, 5.1 and 5.1.1, and on the other hand there is something that I don't like and can't understand with Ortwin's approach, it's the use of the method <code>[webView sizeThatFits:CGSizeZero]</code> with the parameter <code>CGSizeZero</code> : If you read Apple Official documentation about this methods and its parameter, it says clearly : </p> <blockquote> <p>The default implementation of this method returns the size portion of the view’s bounds rectangle. Subclasses can override this method to return a custom value based on the desired layout of any subviews. For example, a UISwitch object returns a fixed size value that represents the standard size of a switch view, and a UIImageView object returns the size of the image it is currently displaying.</p> </blockquote> <p>What I mean is that it's like he came across his solution without any logic, because reading the documentation, the parameter passed to <code>[webView sizeThatFits: ...]</code> should at least have the desired <code>width</code>. With his solution, the desired width is set to the <code>webView</code>'s frame before calling <code>sizeThatFits</code> with a <code>CGSizeZero</code> parameter. So I maintain this solution is working on iOS 6 by "chance".</p> <p>I imagined a more rational approach, which has the advantage of working for iOS 5.0 and later... And also in complex situations where more than one webView (With its property <code>webView.scrollView.scrollEnabled = NO</code> is embedded in a <code>scrollView</code>.</p> <p>Here is my code to force the Layout of the <code>webView</code> to the desired <code>width</code> and get the corresponding <code>height</code> set back to the <code>webView</code> itself:</p> <pre><code>- (void)webViewDidFinishLoad:(UIWebView *)aWebView { aWebView.scrollView.scrollEnabled = NO; // Property available in iOS 5.0 and later CGRect frame = aWebView.frame; frame.size.width = 200; // Your desired width here. frame.size.height = 1; // Set the height to a small one. aWebView.frame = frame; // Set webView's Frame, forcing the Layout of its embedded scrollView with current Frame's constraints (Width set above). frame.size.height = aWebView.scrollView.contentSize.height; // Get the corresponding height from the webView's embedded scrollView. aWebView.frame = frame; // Set the scrollView contentHeight back to the frame itself. } </code></pre> <p>Note that in my example, the <code>webView</code> was embedded in a custom <code>scrollView</code> having other <code>webViews</code>... All these <code>webViews</code> had their <code>webView.scrollView.scrollEnabled = NO</code>, and the last piece of code I had to add was the calculation of the <code>height</code> of the <code>contentSize</code> of my custom <code>scrollView</code> embedding these <code>webViews</code>, but it was as easy as summing my <code>webView</code>'s <code>frame.size.height</code> computed with the trick described above...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload