title: iOS Swift - Multiple activity indicator (spinners) appear in webview
body: <p>I'm working on a webView based screen and I want to have an activity indicator to spin while the web content is loading. </p>
  
  <p>Since I have to use the same activity indicator in another screen of the app, I've put the code in static functions in a specific file. </p>
  
  <p>The activity indicator seems to work fine for some web (simple) web pages but I have an issue when I load more complex pages. <strong>The activity indicators gets duplicate several times.</strong> <em>(See screenshot below)</em></p>
  
  <p>On the screenshot, the first activity indicator has the correct layout but the one below is darker which implies that several other activity indicators have been overlaid on top of each other. <strong>And then they never disappears.</strong> </p>
  
  <p>When it comes to code: </p>
  
  <p>I have a webView and two delegate methods controlling the activity indicators.</p>
  
  <pre><code>func webViewDidStartLoad(_ webView: UIWebView) {
  
      DispatchQueue.main.async {
  
          EBUtil.startActivityIndicator(self)
      }
  }
  
  func webViewDidFinishLoad(_ webView: UIWebView) {
  
      DispatchQueue.main.async {
  
          EBUtil.stopActivityIndicator(self)
  
      }
  }
  </code></pre>
  
  <p>I guess it comes from the fact that webViewDidStartLoad gets called several times. Any idea how I can prevent this behaviour to happen? </p>
  
  <p>Thanks in advance.</p>
  
  <p>Edouard</p>
  
  <p><a href="https://i.stack.imgur.com/au2MV.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/au2MV.jpg" alt="enter image description here"></a></p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>Here is the full code for my VC. </p>
  
  <pre><code>class NewsDetailsViewController: UIViewController, UIWebViewDelegate {
  
  //MARK: - @IBOUTLETS
  
  @IBOutlet weak var webView: UIWebView!
  
  //MARK: - VARIABLES 
  
  var url: String!
  
  //MARK: - APP LIFE CYCLE
  
  override func viewDidLoad() {
      super.viewDidLoad()
  
      if url != nil {
  
          let urlToLoad = NSURL(string: url)
  
          webView.loadRequest(NSURLRequest(url: urlToLoad as! URL) as URLRequest)
      }
  }
  
  func webViewDidStartLoad(_ webView: UIWebView) {
  
      DispatchQueue.main.async {
  
          EBUtil.startActivityIndicator(self)
      }
  }
  
  func webViewDidFinishLoad(_ webView: UIWebView) {
  
      DispatchQueue.main.async {
  
          EBUtil.stopActivityIndicator(self)
  
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>And code for activity indicator start and stop: </p>
  
  <pre><code> static func startActivityIndicator(_ sender: UIViewController) {
  
  
      print("START ANIMATING")
  
      if let view = sender.view {
  
          activityIndicatorBackground = UIView(frame: CGRect(x: view.center.x - 25, y: view.center.y - 25, width: 50, height: 50))
          activityIndicatorBackground.backgroundColor = UIColor.black
          activityIndicatorBackground.layer.zPosition = CGFloat(MAXFLOAT-1)
          activityIndicatorBackground.alpha = 0.6
          activityIndicatorBackground.layer.cornerRadius = 5
  
          view.addSubview(activityIndicatorBackground)
          activityIndicator = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
          activityIndicator.center = view.center
          activityIndicator.hidesWhenStopped = true
          activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.white
          activityIndicator.layer.zPosition = CGFloat(MAXFLOAT)
          view.addSubview(activityIndicator)
          activityIndicator.startAnimating()
          UIApplication.shared.beginIgnoringInteractionEvents()
      }
  }
  
  static func stopActivityIndicator(_ sender: UIViewController) {
      print("STOP ANIMATING")
      activityIndicatorBackground.removeFromSuperview()
      activityIndicator.stopAnimating()
      UIApplication.shared.endIgnoringInteractionEvents()
  }
  </code></pre>