UIActivityIndi​​catorView没有显示在iOS UIViewController中

时间:2018-12-31 14:30:43

标签: ios swift uiactivityindicatorview

我想在视图中心的视图控制器中显示灰色UIViewIndicatorView,但是这段代码没有显示任何内容:

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()
    }   
}

仅在将视图控制器的视图设置为白色(不包括黑色)之类的颜色时,才会显示活动指示器视图。

如何显示它?

1 个答案:

答案 0 :(得分:1)

将背景更改为

view.backgroundColor = .green // for example 

activityIndicatorView.style = .white

然后您将看到它,因为它几乎是指示器和背景视图之间的颜色匹配