Skip to content

Instantly share code, notes, and snippets.

View vishalkevin11's full-sized avatar
💥
Fluttering

vishalkevin11

💥
Fluttering
View GitHub Profile
@pkuecuekyan
pkuecuekyan / WKWebViewSizeToFit.swift
Last active June 17, 2024 17:44
Adjust height of WKWebView frame based on scrollHeight of the webView's content
// Since the WKWebView has no sizeToFit() method, increase the frame height of the webView to
// match the height of the content's scrollHeight
//
// The WKWebView's `navigationDelegate` property needs to be set for the delegate method to be called
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
if webView.isLoading == false {
webView.evaluateJavaScript("document.body.scrollHeight", completionHandler: { [weak self] (result, error) in
if let height = result as? CGFloat {