2013-01-11 27 views
3

我已經嘗試了一堆「解決方案」,但現在我試圖找出UIWebView的滾動視圖的內容大小。它目前總是返回1024,這是設備的寬度。這沒有任何意義,因爲我正在查詢的高度和視圖是縱向的。關於IOS 5 UIWebView報告滾動視圖的內容大小錯誤

下面的代碼報告高度1024.00000

-(void)webViewDidFinishLoad:(UIWebView *)webView { 

    float sourcesWebViewHeight = [[webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.scrollHeight"] floatValue]; 

    NSLog(@"%f", sourcesWebViewHeight); 

} 

而我有的只是文字的幾行,這不作出任何意義,我。 :(

+0

document.body.offsetHeight – Srikanth

+0

在這裏問了一個類似的問題「http://stackoverflow.com/questions/2979854/uiwebview-get-total-height-of-contents-using-javascript」 – Srikanth

回答

11

我會打破什麼終於解決了這對我來說。

我不得不換我這個內容。

<html><head><meta name=\"viewport\" content=\"initial-scale=1, user-scalable=no, width=device-width\" /></head><body>%@</body></html> 

實現以下觀點做負載。

-(void)webViewDidFinishLoad:(UIWebView *)webView { 

    [self layoutSubviews]; 

    webView.scrollView.scrollEnabled = NO; // Property available in iOS 5.0 and later 
    CGRect frame = webView.frame; 


    frame.size.height = 1;  // Set the height to a small one. 

    webView.frame = frame;  // Set webView's Frame, forcing the Layout of its embedded scrollView with current Frame's constraints (Width set above). 

    frame.size.height = webView.scrollView.contentSize.height; // Get the corresponding height from the webView's embedded scrollView. 

    webView.frame = frame; 





} 

and

-(void) layoutSubviews { 
    [super layoutSubviews]; 
    [body stringByEvaluatingJavaScriptFromString: 
    [NSString stringWithFormat: 
    @"document.querySelector('meta[name=viewport]').setAttribute('content', 'width=%d;', false); ", 
    (int)body.frame.size.width]]; 
} 

,最後我的webview做所有正確的事情來擴展自己的內容。

+1

我正要去瘋狂的一個UIWebView,在旋轉到肖像之後寬度有錯誤,這是個訣竅!非常感謝 –

+0

很老的問題,似乎在iOS 9中得到了修復,但對於iOS 8以及之前的版本,手動設置'document.querySelector'是爲我做的伎倆。謝謝! –