Search code examples
iosiphoneuiwebviewswift3

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

My HTML Content is "

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"


Solution

  • Add 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
    
        }
    

    Make sure please check you autolayout constraint also. This method working fine. if your auto layout are not set properly then you can get extra length in it.