我的UI上有一个WebView,仅在实例化期间加载。但是,如果我提供一个操作表,它将再次加载它的内容。知道为什么吗?
struct DetailView: View {
let url: URL
private let NC = NotificationCenter.default
@State private var showingSheet = false
@Environment(\.presentationMode) private var presentationMode
var webView: WebView
init(url: URL){
self.url = url
webView = WebView(url: url)
}
var body: some View {
ZStack{
ShakableViewRepresentable().allowsHitTesting(false).onAppear(){
self.NC.addObserver(forName: Constants.shakeNotification, object: nil, queue: nil, using: self.shakeDetected)
}
webView
.padding( .bottom, -40.0)
.padding(.top , -45.0)
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .topLeading)
.edgesIgnoringSafeArea(.all)
.actionSheet(isPresented: $showingSheet) {
ActionSheet(title: Text("What do you want to do?"), message: Text("There's only one choice..."), buttons: [.default(Text("Restart")), .default(Text("Exit"), action: {
self.presentationMode.wrappedValue.dismiss()
}), .cancel()])
}
}
.hideNavigationAndStatusBar()
}
func shakeDetected(_ notification: Notification) {
showingSheet = true
}
}
一些注意事项:使用ShakableViewRepresentable可以检测到摇动手势。如果检测到这种情况,请出示操作说明书。 hideNavigationAndStatusBar()完成了它的工作。
编辑: 这是我的WebView:
struct WebView: UIViewRepresentable{
var url: URL
func makeUIView(context: UIViewRepresentableContext<WebView>) -> WKWebView {
return WKWebView()
}
func updateUIView(_ uiView: WKWebView, context: UIViewRepresentableContext<WebView>) {
let request = URLRequest(url: url)
uiView.load(request)
}
}