UIActivityIndicator is showing behind everything else in my view

910 views Asked by At

I am trying to get my UIActivityIndicator to show on top of everything. Unfortunately, I cannot get it to do so. It shows up like so (actual spinner is on top, but background is behind everything):

enter image description here

It shows behind everything in the view. Here is my code:

var activityIndicator:UIActivityIndicatorView = UIActivityIndicatorView(frame: CGRectMake(0, 0, 150, 150))
self.activityIndicator.center = self.view.center
self.activityIndicator.hidesWhenStopped = true
self.activityIndicator.backgroundColor = (UIColor(white: 0.0, alpha: 0.8))
self.activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.WhiteLarge
self.activityIndicator.layer.cornerRadius = 5
view.addSubview(self.activityIndicator)
activityIndicator.startAnimating()

How can I make my activityIndicator background to show up on top of everything (as the spinner is doing so)?

Thanks for all your help in advance!

Cheers!

2

There are 2 answers

0
ndmeiri On BEST ANSWER

After adding self.activityIndicator to the view hierarchy, you could call view.bringSubviewToFront(self.activityIndicator).

1
Craig Otis On

You can go through the process of adding a background color to your UIActivityIndicatorView, but it's a stop-gap solution in my opinion.

Instead, I would strongly recommend using MBProgressHUD.

It's open source, easy to drop in (includes CocoaPods support), and is incredibly easy to use. It's a much nicer user experience than dealing with your own background colors, fading/animation, rounded corners, etc. and smooshing it on top of everything else in your view stack.