WKWebView 没有从一开始就在 NSViewRepresentable 中显示
WKWebView doesn't show from the begin in NSViewRepresentable
我将 WKWebView
放入 View
时遇到问题。如您所见,网页从一开始就没有显示。
当我更改应用 window 大小时,页面正确显示。
可能是什么问题?
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()
}
}
如果我添加多行文本,WKWebView 会更加恍惚。
struct WKWebViewDemo: View {
var body: some View {
VStack {
Text("line\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\n")
WebView()
}
}
}
你可以找到我的解决方案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()
}
我将 WKWebView
放入 View
时遇到问题。如您所见,网页从一开始就没有显示。
当我更改应用 window 大小时,页面正确显示。
可能是什么问题?
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()
}
}
如果我添加多行文本,WKWebView 会更加恍惚。
struct WKWebViewDemo: View {
var body: some View {
VStack {
Text("line\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\nline\n")
WebView()
}
}
}
你可以找到我的解决方案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()
}