【问题标题】:How to change the height of webView dynamically?如何动态改变webView的高度?
【发布时间】:2013-06-20 03:50:46
【问题描述】:

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

【问题讨论】:

  • self.webView.frame.size.height 是你想要的吗?

标签: ios webview


【解决方案1】:

试试这个

    - (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】:

    在委托方法 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);
    }
    

    【讨论】:

      【解决方案3】:

      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:我想感谢原创作品。我仍在搜索中

      【讨论】:

        【解决方案4】:
        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

        【讨论】:

        • 这不是“动态的”,您只需在设备为 iPhone 4 时将 webView 的高度固定为 350。
        猜你喜欢
        • 1970-01-01
        • 2013-01-20
        • 1970-01-01
        • 1970-01-01
        • 2018-09-13
        • 1970-01-01
        • 2012-12-22
        • 1970-01-01
        相关资源
        最近更新 更多