WKWebView doesn't show from the begin in NSViewRepresentable

Viewed 371

I face an issue when I put WKWebView to View. As you can see Web page doesn't show from the begin.

enter image description here

When I change the app window size the page shows correctly.

enter image description here

What could be the problem?

import SwiftUI
import WebKit

struct WebView: NSViewRepresentable {
    func makeNSView(context: Context) -> WKWebView  {
        let view = WKWebView()
        guard let url = URL(string: "https://github.com/filimo/ReaderTranslator") else { return view }
        view.load(URLRequest(url: url))
        return view
    }

    func updateNSView(_ view: WKWebView, context: Context) {

    }
}

struct WKWebViewDemo: View {
    var body: some View {
        WebView()
    }
}

If I add Text with multiple lines WKWebView is tranced more.

struct WKWebViewDemo: View {
    var body: some View {
        VStack {
            Text("line\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\n")
            WebView()
        }
    }
}

enter image description here

1 Answers

You can find my solution here

import SwiftUI

struct WebViewContainer<Content>: View where Content: View {
    private let content: () -> Content

    init(@ViewBuilder content: @escaping () -> Content) {
        self.content = content
    }

    var body: some View {
        GeometryReader { geometry in
            ScrollView {
                self.content().frame(height: geometry.size.height)
            }
        }
    }
}

WebViewContainer {
    ViewRepresenter()
}
Related