I\'ve been looking around and wasnt able to see any swift related ways to do this. I\'m trying to get my UIWebViews height to be dynamic. I have a UIWebView that loads data
you can use this class for makking webview size to fit for swift 3 and swift 4
//
// RSWebView.swift
// CustumWebViewDemo
//
// Created by Ruchin Somal on 01/01/18.
// Copyright © 2018 Ruchin Somal. All rights reserved.
//
import UIKit
class RSWebView: UIWebView, UIWebViewDelegate {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)!
self.scrollView.isScrollEnabled = false
self.scrollView.bounces = false
self.delegate = self
}
override init(frame: CGRect) {
super.init(frame: frame)
self.scrollView.isScrollEnabled = false
self.scrollView.bounces = false
self.delegate = self
}
override func loadHTMLString(_ string: String?, baseURL: URL?) {
let s:String = "<html><head><title></title><meta name=\"viewport\" content=\"initial-scale=1, user-scalable=no, width=device-width\" /><link rel=\"stylesheet\" href=\"./webview.css\" ></link></head><body>"+string!+"</body></html>";
var url:URL
if baseURL == nil {
url = Bundle.main.bundleURL
} else {
url = baseURL!
}
super.loadHTMLString(s, baseURL: url)
}
func webViewDidFinishLoad(_ webView: UIWebView) {
self.webViewResizeToContent(webView: webView)
}
func webViewResizeToContent(webView: UIWebView) {
webView.layoutSubviews()
// Set to initial value of webview
var frame:CGRect = webView.frame
frame.size.height = 1.0
webView.frame = frame
// Calculated height of webview
let wvheight: CGFloat = webView.scrollView.contentSize.height
print("UIWebView.height: \(wvheight)")
var wvRect = webView.frame
wvRect.size.height = wvheight
webView.frame = wvRect
let heightConstraint = NSLayoutConstraint(item: webView, attribute: NSLayoutAttribute.height, relatedBy: NSLayoutRelation.equal, toItem: nil, attribute: NSLayoutAttribute.height, multiplier: 1.0, constant: wvheight)
webView.addConstraint(heightConstraint)
webView.window?.setNeedsUpdateConstraints()
webView.window?.setNeedsLayout()
}
}
this only worked for me
func webViewDidFinishLoad(_ webView: UIWebView) {
webView.frame.size.height = 1
webView.frame.size = webView.sizeThatFits(.zero)
webView.scrollView.isScrollEnabled=false;
myWebViewHeightConstraint.constant = webView.scrollView.contentSize.height
webView.scalesPageToFit = true
}
make sure you've created an outlet for myWebViewHeightConstraint
I'm using HTML string in my load and have to use the following method in Webview delegate:
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.evaluateJavaScript("document.documentElement.scrollHeight", completionHandler: { (height, error) in
self.webviewHeightConstraint?.constant = height as! CGFloat
})
}
and the full code:
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
@IBOutlet weak var webviewHeightConstraint: NSLayoutConstraint!
@IBOutlet weak var webview: WKWebView!
var observing = false
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
webview.navigationDelegate = self
webview.scrollView.isScrollEnabled = false
self.webview.loadHTMLString("""
<html>
<head>
<style>
ul {
list-style: none;
}
ul li::before {
content: "\\2022";
color: red;
font-weight: bold;
display: inline-block;
width: 1em;
margin-left: -1em;
}
</style>
</head>
<body>
<h2>Change Bullet Color of List Items</h2>
<ul>
<li>Adele</li>
<li>Agnes</li>
<li>Billy</li>
<li>Bob</li>
<li>Bob</li>
<li>Bob</li>
<li>Bob</li>
<li>Bob</li>
<li>Bob</li>
<li>Bob</li>
<li>Bob</li>
<li>Bob</li>
<li>Bob</li>
<li>Bob</li>
</ul>
</body>
</html>
""", baseURL: nil)
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.evaluateJavaScript("document.documentElement.scrollHeight", completionHandler: { (height, error) in
self.webviewHeightConstraint?.constant = height as! CGFloat
})
}
}