【问题标题】:On webViewDidFinishLoad i am getting extra height of content in swift3在 webViewDidFinishLoad 上,我在 swift3 中获得了额外的内容高度
【发布时间】:2016-09-23 06:10:12
【问题描述】:
  EventWebView.frame.size = EventWebView.sizeThatFits(CGSize.zero)
  EventWebView.scrollView.isScrollEnabled = false;    
  var frame = EventWebView.frame;

  frame.size.width = EventTableView.frame.size.width-40;       // Your desired width here.
  frame.size.height = 1;        // Set the height to a small one.

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

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

我的 HTML 内容是 "

Tester 新创建的事件的测试描述。测试人员新创建的事件的测试说明。测试人员新创建的事件的测试说明。测试人员对新创建的事件的测试描述。测试 Tester.Testing 新创建事件的描述 Tester.Testing 测试新创建事件的描述。测试 Tester.Testing 新创建事件的描述 Tester.Testing 测试新创建事件的描述。测试 Tester.Testing 新创建事件的描述 Tester.Testing 测试新创建事件的描述。测试 Tester.Testing 新创建事件的描述 Tester.Testing 测试新创建事件的描述。测试人员新创建事件的测试说明。测试人员新创建事件的测试说明。

\n"

【问题讨论】:

  • 我遇到了同样的错误。当我在约束中说 200 时,我得到了 1000 的宽度和高度。在 viewDidLoad 我必须添加以下行 let rect = CGRect(x: 0, y: 0, width: 200, height: 200) // CGFloat, Double, Int myView.frame = rect
  • @Imran 请检查您的自动布局约束。我的问题在于自动布局约束。

标签: ios iphone uiwebview swift3


【解决方案1】:

添加 WebViewDelegate

func webViewDidFinishLoad(_ webView: UIWebView) {

        webView.frame.size = webView.sizeThatFits(CGSize.zero)
        webView.scrollView.isScrollEnabled = false;    // Property available in iOS 5.0 and later
        var frame = webView.frame;

        frame.size.width = self.view.frame.width-20;       // Your desired width here.
        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;
        fl = webView.frame.size.height

    }

请确保也检查您的自动布局约束。这种方法工作正常。如果您的自动布局设置不正确,那么您可以在其中获得额外的长度。

【讨论】:

    【解决方案2】:

    试试这个

    NSString *contentHeight = [webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight;"];

    int height = [contentHeight integerValue];

    在您的 webViewDidFinishLoad 方法中。确保为 Web 视图设置委托并在视图控制器中实现上述委托方法。

    【讨论】:

      猜你喜欢
      • 2021-09-13
      • 1970-01-01
      • 2012-05-12
      • 2012-03-14
      • 1970-01-01
      • 2012-01-11
      • 2015-04-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多