以下のコードでwebviewの初回読み込み時には、progress barが意図した動きになるのですが、webview内のリンクをふむとprogress barが2つ表示されているように見えてしまいます。
どなたかおわかりになる方はいらっしゃいませんでしょうか?
Swift
1class DetailViewController: UIViewController { 2 3 private var wkWebView: WKWebView! 4 5 let progressView = UIProgressView(progressViewStyle: .default) 6 7 private var estimatedProgressObserver: NSKeyValueObservation? 8 9 override func viewDidLoad() { 10 super.viewDidLoad() 11 12 setupWebView() 13 load() 14 setupProgressView() 15 setupEstimatedProgressObserver() 16 } 17 18 19 20 func setupWebView() { 21 let webConfig = WKWebViewConfiguration() 22 wkWebView = WKWebView(frame: .zero, configuration: webConfig) 23 wkWebView.uiDelegate = self 24 wkWebView.navigationDelegate = self 25 } 26 27 28 private func setupProgressView() { 29 guard let navigationBar = navigationController?.navigationBar else { return } 30 31 progressView.translatesAutoresizingMaskIntoConstraints = false 32 navigationBar.addSubview(progressView) 33 34 progressView.isHidden = true 35 36 NSLayoutConstraint.activate([ 37 progressView.leadingAnchor.constraint(equalTo: navigationBar.leadingAnchor), 38 progressView.trailingAnchor.constraint(equalTo: navigationBar.trailingAnchor), 39 40 progressView.bottomAnchor.constraint(equalTo: navigationBar.bottomAnchor), 41 progressView.heightAnchor.constraint(equalToConstant: 2.0) 42 ]) 43 } 44 45 private func setupEstimatedProgressObserver() { 46 estimatedProgressObserver = wkWebView.observe(.estimatedProgress, options: [.new]) { [weak self] webView, _ in 47 self?.progressView.progress = Float(webView.estimatedProgress) 48 } 49 } 50} 51 52 53// MARK: - WKNavigationDelegate 54extension DetailViewController: WKNavigationDelegate { 55 56 57 // 読み込み準備開始 58 func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) { 59 if progressView.isHidden { 60 progressView.isHidden = false 61 } 62 63 UIView.animate(withDuration: 0.33, animations: { 64 self.progressView.alpha = 1.0 65 }) 66 print("読み込み準備開始") 67 } 68 69 // 読み込み完了 70 func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) { 71 UIView.animate(withDuration: 0.33, 72 animations: { 73 self.progressView.alpha = 0.0 74 }, 75 completion: { isFinished in 76 self.progressView.isHidden = isFinished 77 }) 78 print("読み込み完了") 79 } 80}
環境
Swift5
iPhone SE(2nd generation) Version 11.6 (921.9.1)
xcode 11.6
あなたの回答
tips
プレビュー