Cesare
Cesare

Reputation: 9409

UIActivityIndicatorView not showing up in iOS UIViewController

I want to display a gray UIViewIndicatorView in a view controller at the center of the view but this piece of code isn't showing anything:

class MyViewController: UIViewController {
    var activityIndicatorView = UIActivityIndicatorView()

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .black
        displayActivityIndicator()
    }

    func displayActivityIndicator() {
        view.addSubview(activityIndicatorView)
        activityIndicatorView.style = .gray
        activityIndicatorView.translatesAutoresizingMaskIntoConstraints = false
        activityIndicatorView.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
        activityIndicatorView.widthAnchor.constraint(equalToConstant: 40).isActive = true
        activityIndicatorView.heightAnchor.constraint(equalToConstant: 40).isActive = true
        activityIndicatorView.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
        activityIndicatorView.startAnimating()
    }   
}

This only shows up the activity indicator view when I set the view controller's view to a color like white (excluding black).

How can I make it show up?

Upvotes: 1

Views: 998

Answers (1)

Shehata Gamal
Shehata Gamal

Reputation: 100503

Change background to

view.backgroundColor = .green // for example 

or

activityIndicatorView.style = .white

then you' ll see it , as it's nearly a color match between the indicator and the background view

Upvotes: 1

Related Questions