【发布时间】:2021-11-04 16:20:49
【问题描述】:
我添加了这段代码以便能够在 SwiftUI 中使用 WebView:
extension WKWebView {
func load(_ urlString: String) {
if let url = URL(string: urlString) {
let request = URLRequest(url: url)
load(request)
}
}
}
struct WebView : UIViewRepresentable {
@State var url: String
func makeUIView(context: Context) -> WKWebView {
return WKWebView()
}
func updateUIView(_ uiView: WKWebView, context: Context) {
uiView.load(url)
}
}
但是,当单击按钮时,我需要导航到 URL。
struct ContentView: View {
@State private var showLoginForm: Bool = true
...
var webView = WebView(url: "https://www.google.com")
...
var body: some View {
if showLoginForm {
...
Button(action: {
showLoginForm = false
webView.url = "https://www.example.com/go/1"
}) { ... }
} else {
webView
}
}
}
但webView 停留在初始页面上
【问题讨论】:
-
下一步应该会有所帮助stackoverflow.com/a/61059318/12299030, stackoverflow.com/a/65465719/12299030, stackoverflow.com/a/63071151/12299030 .. 只需通过
WKWebView在这里搜索更多内容