1

我现在用webView来展示新闻,新闻下面我打算放一些按钮。所以我需要得到webView的高度,以便在全部下载后根据webView的高度来改变按钮的位置。在其他话说,如何动态改变webView的高度?

4

4 回答 4

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];
        }
    }
于 2013-06-20T04:40:10.423 回答
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);
}
于 2013-06-20T04:20:46.320 回答
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:我要感谢原创作品。我仍在搜索

于 2013-06-20T04:39:37.147 回答
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

于 2013-11-18T10:45:57.913 回答