繁体   English   中英

如何获得适当的UIWebView内容高度?

[英]how to get proper UIWebView content height?

我在视图中添加UIWebView,在ScrollView中添加该视图。

我想获取UIWebView内容的框架。 那么我可以将框架设置为查看和滚动视图吗?

我知道我们可以使用javascript获取高度。 如下所述:

float height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue];

但是,当我旋转视图并通过didRotateFromInterfaceOrientation::(UIInterfaceOrientation)fromInterfaceOrientation方法在didRotateFromInterfaceOrientation::(UIInterfaceOrientation)fromInterfaceOrientation重新计算UIWebView内容的高度时,则增加了内容高度。

因此,请建议我旋转方向时如何获得合适的高度?

谢谢。

采用这种方法:

- (void) webViewDidFinishLoad:(UIWebView *)webView{
    CGRect frame = wvContent.frame;
    CGSize fittingSize = [wvContent sizeThatFits:CGSizeZero];
    frame.size = fittingSize;
    wvContent.frame = frame;
}

然后在每次设备旋转时调用此方法:

- (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的委托方法中添加一种计算高度的方法

- (void)webViewDidFinishLoad:(UIWebView *)webView{
    float height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue];
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM