我在 View 中添加 UIWebView,在 ScrollView 中添加 That view。
我想获取 UIWebView 内容的框架。那我可以设置frame来view和scrollview吗?
我知道我们可以使用 javascript 获取高度。如下所述:
float height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"] floatValue];
但是当我旋转 View 并在 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
}
希望有所帮助:)
关于javascript - 如何获得正确的 UIWebView 内容高度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12087002/