Reputation: 33
How would I set the text size of a SwiftUI WebKit View to a @State variable? I have this code that allows me to use WebViews in SwiftUI
import SwiftUI
import WebKit
struct WebView : UIViewRepresentable {
var url : URL;
func makeUIView(context: Context) -> WKWebView {
return WKWebView();
}
func updateUIView(_ uiView: UIViewType, context: Context) {
uiView.loadFileURL(url, allowingReadAccessTo: url)
}
}
I saw this and tried to replicate it,
class Coordinator : NSObject {
func webView(_ webView: WKWebView, js : String) {
webView.evaluateJavaScript(js)
}
}
But I wasn't able to get it to work.
Thanks in advance.
Upvotes: 3
Views: 2498
Reputation: 257829
You can try to do this in navigation delegate, after loading did finish, as shown below:
class Coordinator : NSObject, WKNavigationDelegate {
// ... any other code
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
webView.evaluateJavaScript(js, completionHandler: { (value, error) in
// .. do anything needed with result, if any
})
}
}
struct WebView : UIViewRepresentable {
var url : URL;
func makeUIView(context: Context) -> WKWebView {
let webview = WKWebView()
webview.navigationDelegate = context.coordinator
return webview
}
func makeCoordinator() -> Coordinator {
Coordinator()
}
func updateUIView(_ uiView: UIViewType, context: Context) {
uiView.loadFileURL(url, allowingReadAccessTo: url)
}
}
Upvotes: 1