我目前正在使用webView来显示新闻,在我打算放置一些按钮的新闻下面。所以我需要获得webView的高度,以便在下载完毕后根据webView的高度更改按钮的位置换句话说,如何动态更改webView的高度?
答案 0 :(得分:4)
在委托方法webViewDidFinishLoad中执行此操作:
- (void)webViewDidFinishLoad:(UIWebView *)aWebView
{
CGFloat height = [[aWebView stringByEvaluatingJavaScriptFromString:@"document.height"] floatValue];
CGFloat width = [[aWebView stringByEvaluatingJavaScriptFromString:@"document.width"] floatValue];
aWebView.frame = CGRectMake(aWebView.frame.origin.x,aWebView.frame.origin.y,width, height);
}
答案 1 :(得分:4)
试试这个
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
NSString *string = [_webView stringByEvaluatingJavaScriptFromString:@"document.getElementById(\"body\").offsetHeight;"];
CGFloat height = [string floatValue] + 8;
CGRect frame = [_webView frame];
frame.size.height = height;
[_webView setFrame:frame];
if ([[self delegate] respondsToSelector:@selector(webViewController:webViewDidResize:)])
{
[[self delegate] webViewController:self webViewDidResize:frame.size];
}
}
答案 2 :(得分:1)
webView具有方法sizeThatFits:
,一旦在视图层次结构中添加webView并完成加载html,它就会返回内容的大小。
但如果没有警告,这不起作用。在调用拟合大小方法之前,需要将webview的框架设置为较低的值。
例如:如果你有一个固定的宽度和可变的高度
//Set a very low height
CGRect webViewFrame = self.webView.frame;
webViewFrame.size.height = 5.0f;
self.webView.frame = webViewFrame;
//Re calculate the size that fits
CGSize size = [self.webView sizeThatFits:webViewFrame.size];
self.webViewFrame.size = size;
self.webView.frame = webViewFrame;
PS:我想赞扬原作。我还在搜索SO
答案 3 :(得分:1)
According to your device type like iPhone 4 or iPhone 5 you can set your webview
The web view has finished loading.
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[DejalActivityView removeView];
if ([objSpineCustomProtocol windowHeight] == 480) {
termsWebView.frame = CGRectMake(termsWebView.frame.origin.x
,termsWebView.frame.origin.y
,termsWebView.frame.size.width
,350);
}
}// End of Method webViewDidFinishLoad
// VKJ