简体   繁体   English

如何阻止 SwiftUI 重新加载我通过 UIViewRepresentable 使用的 WKWebView 页面?

[英]How do I stop SwiftUI from reloading WKWebView page that I am using via UIViewRepresentable?

My code sets the navigation bar title it gets from web page, but everytime this causes the whole WKWebView to reload... The page is quite heavy so consumes a lot of time.我的代码设置了它从 web 页面获取的导航栏标题,但是每次这都会导致整个 WKWebView 重新加载......页面很重所以会消耗很多时间。 Same behaviour when using button to set title.使用按钮设置标题时的行为相同。


struct TransitScreen: View, HandlerDelegate {

    var urlRequest: URLRequest
    @State var pageTitle = "Title 1"

    var body: some View {

        VStack {

            KSWebView(urlRequest: urlRequest, delegate: self) // WKWebView using UIViewRepresentable
             
            NavigationLink("Go", destination: Text("Some Data"))
                
            Button(action: {
               pageTitle = "replaced \(Date().toSeconds())"
            }){
               Text("Change title")
            }
            
        }.navigationBarTitle(Text(pageTitle))
        .edgesIgnoringSafeArea(.top)
    }


    func process(data: String){
        pageTitle = data
    }

}

Is there anyway to stop this from happening?有没有办法阻止这种情况发生? or another method to update navigationBarTitle?或其他更新navigationBarTitle 的方法?

Found source of problem找到问题根源

// KSWebView.swift

 func updateUIView(_ uiView: WKWebView, context: Context) {
   uiView.load(urlRequest)
  }

changed this to将其更改为


func makeUIView(context: Context) -> WKWebView {
        // TODO: Check this hacky code and find some replacement
        let headerHeight = CGFloat(100)
        let webViewHeight = UIScreen.main.bounds.height - headerHeight
        let webViewFrame = CGRect(x: 0, y: headerHeight, width: UIScreen.main.bounds.width, height: webViewHeight)

        let webView = WKWebView(frame: webViewFrame, configuration: config())
        webView.load(urlRequest)
        self.webView = webView
        return webView
    }

    func updateUIView(_ uiView: WKWebView, context: Context) {

    }

This stops the view from loading request everytime its updated.这会阻止视图在每次更新时加载请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 SwiftUI 中使用 WKWebView 时如何拦截链接导航? - How do I intercept link navigation when using WKWebView in SwiftUI? SwiftUI:如何从 UIViewRepresentable 导航到 SwiftUI 视图 - SwiftUI: How to navigate from UIViewRepresentable to SwiftUI View 如何使用 swiftui 从 appdelegate 访问 wkwebview? - how can i access wkwebview from appdelegate with swiftui? 如何将所有内容从一个WKWebView复制到新的WKWebView - How do i copy everything from one WKWebView to a new WKWebView 如何检查我是否在科尔多瓦应用程序中使用 WKWebview 而不是 UIWebview - How to check am I using WKWebview instead of UIWebview in cordova app Swift-如果与以前的图像相同,如何阻止表重新加载图像? - Swift - How do I stop a table from reloading an image if it's the same image as before? 每次从 SwiftUI 中的详细视图返回时,如何阻止视图刷新? - How do I stop a view from refreshing each time I return from a detail view in SwiftUI? 如何在 SwiftUI 中结束对 UIViewRepresentable 的编辑? - How to endEditing on UIViewRepresentable in SwiftUI? SwiftUI 中的 WKWebView - 用户与网站交互时如何切换视图? - WKWebView in SwiftUI - How do I switch the view when user interacts with the website? 如何在 UIViewRepresentable 的按钮点击上显示 UIViewControllerRepresentable 屏幕 - How can i show UIViewControllerRepresentable screen on button tap from UIViewRepresentable
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM