简体   繁体   English

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

[英]how to get proper UIWebView content height?

I am add UIWebView in View and That view in ScrollView. 我在视图中添加UIWebView,在ScrollView中添加该视图。

I want to get frame of the UIWebView Content. 我想获取UIWebView内容的框架。 So can I set the frame to view and scrollview? 那么我可以将框架设置为查看和滚动视图吗?

I know we can get the height by using javascript. 我知道我们可以使用javascript获取高度。 As mention below: 如下所述:

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

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

So please suggest me how can I get the proper height when i am rotate the orientation? 因此,请建议我旋转方向时如何获得合适的高度?

Thanks. 谢谢。

Adopt this method: 采用这种方法:

- (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
}

Hope that help :) 希望对您有所帮助:)

Make sure that you are retrieving height when all the content is loaded. 装入所有内容后,请确保要检索身高。

You can add a method to calculate height in UIWebView 's delegates method 您可以在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