I'm trying to use a UIWebView
for displaying content higher than the screen of the iPhone, without needing to scroll in the UIWebView
itself.
The UIWebView
is placed as a subview to a UIScrollView
, along with some other objects that I want the UIScrollView
to scroll up and down with the UIWebView
.
I know you can do this with a UITextView
like this:
CGRect frame = _textView.frame; frame.size.height = _textView.contentSize.height; _textView.frame = frame;
But the UIWebView
does not inherit from UIScrollView
and does therefore not contain the contentSize
-property.
I'd really like to keep it a UIWebView
, because the data I get is in HTML-blocks.
Thank you!
In fact, no Javascript is needed! In UIView there is a nice method:
- (CGSize)sizeThatFits:(CGSize)size
You pass it anything (not really meaningful here), and it returns the size it'd like to be, using its content (subviews, etc) to compute the size.
Of course, since content loading in a UIWebView in asynchronous, you should do this after the webview has loaded, for instance in the UIWebViewDelegate method:
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
CGSize size = [webView sizeThatFits: CGSizeMake(1.0f, 1.0f)]; // Pass about any size
CGRect frame = webView.frame;
frame.size.height = size.height;
webView.frame = frame;
}
Et voila!