在webViewDidFinishLoad上,我在swift3中得到了额外的高度

Raj*_*ant 3 iphone uiwebview ios 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
Run Code Online (Sandbox Code Playgroud)

我的HTML内容是

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

\ n“

Raj*_*ant 5

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

    }
Run Code Online (Sandbox Code Playgroud)

确保也检查自动布局约束。这种方法工作正常。如果您的自动版式设置不正确,则可以得到更多的长度。