簡體   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