The first time the webview is loaded with the following code, the progress bar is the intended movement, but if you look at the link in the webview, it looks like two progress bars are displayed.
Does anyone know?
class DetailViewController:UIViewController {
private var wkWebView —WKWebView!
let progressView=UIProgressView (progressViewStyle: .default)
private variableProgressObserver —NSKeyValueObserver?
override func viewDidLoad(){
super.viewDidLoad()
setupWebView()
load()
setupProgressView()
setupEstimatedProgressObserver()
}
func setupWebView() {
letwebConfig=WKWebViewConfiguration()
wkWebView = WKWebView (frame:.zero, configuration:webConfig)
wkWebView.uiDelegate=self
wkWebView.navigationDelegate=self
}
private func setupProgressView(){
guardlet navigationBar=navigationController?.navigationBarelse {return}
progressView.translatesAutoresizingMaskIntoConstraints=false
navigationBar.addSubview(progressView)
progressView.isHidden=true
NSLayoutConstraint.activate([]
progressView.leadingAnchor.constraint(equalTo:navigationBar.leadingAnchor),
progressView.trailingAnchor.constrain(equalTo:navigationBar.trailingAnchor),
progressView.bottomAnchor.constraint(equalTo:navigationBar.bottomAnchor),
progressView.heightAnchor.constrain (equalToConstant: 2.0)
])
}
private func setupEstimatedProgressObserver(){
estimatedProgressObserver=wkWebView.observe(\.estimatedProgress, options: [.new]){[weakself] webView,_in
self?.progressView.progress=Float(webView.estimatedProgress)
}
}
}
// MARK: - WKNavigationDelegate
extensionDetailViewController:WKNavigationDelegate{
// Read Ready Start
funcwebView(_webView:WKWebView, didStartProvisionalNavigation!){
if progressView.isHidden{
progressView.isHidden=false
}
UIView.animate (withDuration: 0.33, animations: {
self.progressView.alpha=1.0
})
print("Read Ready")
}
// Read Complete
func webView(_webView:WKWebView, DidFinish navigation:WKNavigation!){
UIView.animate (withDuration: 0.33,
animations: {
self.progressView.alpha=0.0
},
completion: {isFinished in
self.progressView.isHidden=isFinished
})
print("Read Complete")
}
}
Swift5
iPhone SE(2nd generation) Version 11.6(921.9.1)
xcode 11.6
The slightly lighter progress bar on the bottom looks like it's being displayed on the website.
So I think the code is working as expected, but I think the website is also displaying its own progress bar, so as a result, two of them appear.
Does the symptom remain the same when I visit another website?
© 2024 OneMinuteCode. All rights reserved.