看不到 Web 视图顶部的按钮



我看不到应该在web视图顶部的按钮,让它后退和前进。我不知道这是网页视图占用了整个屏幕的问题,还是别的什么。我试过用按钮创建一个新视图,然后把视图放进去。我不知道。

下面是我的代码:
import WebKit
import SwiftUI
struct Webview : UIViewRepresentable {

let request: URLRequest
var webview: WKWebView?
init(web: WKWebView?, req: URLRequest) {
self.webview = WKWebView()
self.request = req
}
func makeUIView(context: Context) -> WKWebView  {
return webview!
}
func updateUIView(_ uiView: WKWebView, context: Context) {
uiView.load(request)
}
func goBack(){
webview?.goBack()
}
func goForward(){
webview?.goForward()
}
}

struct ContentView: View {
@State var searchURL = "https://google.com"
var body: some View {
VStack {
HStack {
//MARK: BACK BUTTON
Button(action: {
Webview(web: nil, req: URLRequest(url: URL(string: "(searchURL.prefix(4) == "http" ? searchURL : "https://(searchURL)")")!)).goBack()
}) {
Image("arrowshape.turn.up.backward")
}
//MARK: FOREWARD BUTTON
Button(action: {
Webview(web: nil, req: URLRequest(url: URL(string: "(searchURL.prefix(4) == "http" ? searchURL : "https://(searchURL)")")!)).goForward()
}) {
Image("arrowshape.turn.up.foreward")
}
}
Webview(web: nil, req: URLRequest(url: URL(string: "(searchURL.prefix(4) == "http" ? searchURL : "https://(searchURL)")")!))
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

按钮动作本身不应该是一个视图,而应该只是更新一个变量,该变量存储了webview要显示的URL。然而,这不是必需的。

看看这个。应该涵盖您需要的所有场景,而不需要手动管理url

最新更新