0

I actually created an extension to UIView. So that I just do this with the ViewController's first view:

view.displayBusy()      // you don't need 'self.view' in Swift
...
view.displayNotBusy()

I also tried this:

view.displayBusy()
DispatchQueue.global().async {
   doSomething()
   DispatchQueue.main.async {
      doSomethingComplete()
   }
}
func doSomethingComplete() {
   view.displayNotBusy()
}

Not working either.

And here's my extension. I add the subview, I bring it to the front. But I cannot see it. I also set breakpoints and checked out the frame after the constraints were updated. It is right in the center. And I've tested without the 'displayNotBusy', and with 'hidesWhenStopped' set to false just to make sure it didn't jump right thru the busy area. I made the size 128.

I cannot see it. Does anyone know why I don't see it?

extension UIView {
    func displayBusy() {
        var busyView : UIActivityIndicatorView? = viewWithTag(0x4255) as? UIActivityIndicatorView
        if busyView == nil {
            busyView = UIActivityIndicatorView(frame: CGRect(x: 50, y: 50, width: 32, height: 32))
            busyView!.tag = 0x4255
            addSubview(busyView!)

            busyView!.translatesAutoresizingMaskIntoConstraints = false
            let widthConstraint = NSLayoutConstraint(item: busyView!, attribute: .width, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 32)
            let heightConstraint = NSLayoutConstraint(item: busyView!, attribute: .height, relatedBy: .equal, toItem: nil, attribute: .notAnAttribute, multiplier: 1, constant: 32)
            addConstraints([widthConstraint, heightConstraint])

            let horConstraint = NSLayoutConstraint(item: busyView!, attribute: .centerX, relatedBy: .equal, toItem: self, attribute: .centerX, multiplier: 1, constant: 0)
            let verConstraint = NSLayoutConstraint(item: busyView!, attribute: .centerY, relatedBy: .equal, toItem: self, attribute: .centerY, multiplier: 1, constant: 0)
            addConstraints([horConstraint, verConstraint])

            // Now make it all happen
            setNeedsUpdateConstraints()
            setNeedsLayout()
            layoutIfNeeded()
        }
        self.bringSubview(toFront: busyView!)
        busyView!.hidesWhenStopped = true
        busyView!.isHidden = false
        busyView!.startAnimating()
    }

    func displayNotBusy() {
        let busyView : UIActivityIndicatorView? = viewWithTag(0x4255) as? UIActivityIndicatorView
        if busyView != nil {
            busyView!.stopAnimating()
        }
    }
}
Joe C
  • 2,728
  • 4
  • 30
  • 38
  • 1
    Are you doing the displayBusy and displayNotBusy within the same function/method because if so the display won't be updated before you remove it. – Upholder Of Truth May 22 '18 at 17:54
  • Have you clicked on the Debug View Hierarchy button in XCode to see if it is hiding behind something? – CodeBender May 22 '18 at 17:55
  • 1
    The height/width constraints should be added to the activity itself not the parent view `busyView.addConstraints([widthConstraint, heightConstraint])` it may be the main reason – Shehata Gamal May 22 '18 at 18:03
  • @Sh_Kahn. Thx. I fixed that. But my UIActivityIndicator is still not visible. – Joe C May 22 '18 at 19:54
  • 1
    I tried your code in a new project, and I just made `func doSomething() { sleep(5) }`. The activity indicator appeared, ran for 5 seconds and then disappeared. Is it possible you just don't see it (white on white), or are you trying to do this is a `UITableViewController` perhaps? – vacawama May 22 '18 at 20:26
  • @Sh_Khan. OMG, you're right!. I could have sworn I read that UIActivityIndicators default color was black. This notion that I should explicitly set them to the color black passed thru my mind but I asked myself how many darts do I want to throw on the dark and I never tried that. So yes, my background was white and the UIActivityIndicator was white. Sorry for the mess-up. – Joe C May 23 '18 at 04:49

0 Answers0