如何获得正确的UIWebView内容高度? [英] how to get proper UIWebView content height?
问题描述
我在View中添加了UIWebView,在ScrollView中添加了该视图。
I am add UIWebView in View and That view in ScrollView.
我想获得UIWebView内容的框架。那么我可以设置框架来查看和滚动查看吗?
I want to get frame of the UIWebView Content. So can I set the frame to view and scrollview?
我知道我们可以通过使用javascript获得高度。如下所述:
I know we can get the height by using javascript. As mention below:
float height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue];
但是当我旋转视图并重新计算 didRotateFromInterfaceOrientation ::(UIInterfaceOrientation)fromInterfaceOrientation
方法,然后内容高度增加。
But when I am rotating the view and recalculate the height of the UIWebView content in didRotateFromInterfaceOrientation::(UIInterfaceOrientation)fromInterfaceOrientation
method, then content height was increased.
所以请建议我如何才能获得适当的高度我正在轮换方向?
So please suggest me how can I get the proper height when i am rotate the orientation?
谢谢。
推荐答案
采用这个方法:
- (void) webViewDidFinishLoad:(UIWebView *)webView{
CGRect frame = wvContent.frame;
CGSize fittingSize = [wvContent sizeThatFits:CGSizeZero];
frame.size = fittingSize;
wvContent.frame = frame;
}
然后每次设备旋转时调用此方法:
Then call this method everytime device rotating:
- (void) reloadContent
{
CGRect frame = wvContent.frame;
frame.size.height = 1;
frame.size.width = wvContent.frame.size.width; // you should change webview's width here.
wvContent.frame = frame;
[wvContent loadHTMLString:yourHTML baseURL:nil]; // reload webview
}
希望有帮助:)
这篇关于如何获得正确的UIWebView内容高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!