[英]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.