在 webViewDidFinishLoad 上,我在 swift3 中获得了额外的内容高度

On webViewDidFinishLoad i am getting extra height of content in swift3

  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内容是

Testing Description for the new created event by Tester. Testing Description for the new created event by Tester.Testing Description for the new created event by Tester. Testing Description for the new created event by Tester. Testing Description for the new created event by Tester.Testing Description for the new created event by Tester.Testing Description for the new created event by Tester. Testing Description for the new created event by Tester.Testing Description for the new created event by Tester.Testing Description for the new created event by Tester. Testing Description for the new created event by Tester.Testing Description for the new created event by Tester.Testing Description for the new created event by Tester. Testing Description for the new created event by Tester.Testing Description for the new created event by Tester.Testing Description for the new created event by Tester. Testing Description for the new created event by Tester.Testing Description for the new created event by Tester.

\n"

试试这个

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

int height = [contentHeight integerValue];

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

添加 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

    }

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