Advertise here




Advertise here

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Getting actual content size of UIWebView?

boneheadbonehead Posts: 225Registered Users
edited March 2011 in iPhone SDK Development
I have an embedded UIWebView in my app whose content size can vary a great deal. Before loading content into the view, I set the frame to some reasonable size; however, after the content is loaded, there can be a big gap between the web view and the view that comes after.

Is there some way to automatically resize the web view based on its content? I tried using UIView sizeToFit, but apparently UIWebView just uses the default (no-op) implementation.
Post edited by bonehead on
· ·

Replies

  • boneheadbonehead Posts: 225Registered Users
    edited June 2008
    What I ended up doing was setting the initial height of the web view fairly small, then calling
    [webView sizeThatFits:CGSizeZero];
    

    (from webViewDidFinishLoad) and using this size to resize the web view. This works.
    · ·
  • sergeychsergeych Posts: 1New Users
    edited November 2008
    - tried it with no lock, sizeThatFits called there returns exact same CGRect that was passed at creation time (I've used UIWebView alloc initWithFrame). Could you tell me more how did you managed to get the content height? Thanks in advance, as I'm completely confused. Have to put another view below the web view that may change size :(
    · ·
  • Reidy123Reidy123 Posts: 43Registered Users
    edited April 2009
    Any answer to this as I am in the same boat?
    · ·
  • nielsbotnielsbot Posts: 2New Users
    edited June 2009
    Reidy123 wrote: »
    Any answer to this as I am in the same boat?

    If you call sizeThatFits: from your webViewDidFinishLoad: delegate method, you'll get a correct size. Works for me w/ iPhone 3 SDK.
    · ·
  • hamiseixashamiseixas Posts: 6New Users
    edited July 2009
    It seems that Apple corrected this bug only for SDK 3.0.

    In SDK 2, sizeThatFits returns the frame's size...
    I think we should use the javascript approach:
    [webview stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"foo\").offsetHeight;"];
    

    I know... it is very ugly... :p
    Hami.
    · ·
  • bclerybclery Posts: 2New Users
    edited May 2010
    sizeThatFits does seem to return something more useful IF the UIWebView was actually drawn (was part of a view that is on the screen). When I hold the UIWebView and do not install it into a parent view then the webViewDidFinishLoad is called, but the sizeThatFits is only the given frame.

    I'm still working the best way to get a height from UIWebView too, this seems a common issue.
    · ·
  • hamiseixashamiseixas Posts: 6New Users
    edited May 2010
    bclery wrote: »
    sizeThatFits does seem to return something more useful IF the UIWebView was actually drawn (was part of a view that is on the screen). When I hold the UIWebView and do not install it into a parent view then the webViewDidFinishLoad is called, but the sizeThatFits is only the given frame.

    I'm still working the best way to get a height from UIWebView too, this seems a common issue.

    I'm using Javascript to calculate it and it works very well. But first, the only thing you should do, is to preload any media inside HTML, like images. It can be done using Javascript to.
    · ·
  • alexBBDalexBBD Posts: 1New Users
    edited March 2011
    I know it's an older post, but since I got here looking for a fix I'll post here (it might help future visitors).

    As long as the webview is created with a size larger then the size required to display its content then the solution posted here (sizeThatFits) will return the actual size of the webview. However, if the webview is created with a smaller size then the sizeThatFits will return the content size.
    I only tested it on 4.2, I hope it helps.
    · ·
Sign In or Register to comment.