如何在 UIActivityIndi​​catorView 后面添加黑色背景

How can add black background behind the UIActivityIndicatorView

你好,我想在我的 UIViewController 上显示一个 UIActivityIndicatorView,就像这样

如何绘制像上图这样的 UIViewIndicator

我试过了

func showIndicatorView(){

        let loadingIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50))
        loadingIndicator.layer.cornerRadius = 05

        loadingIndicator.opaque = false
        loadingIndicator.backgroundColor = UIColor(white: 0.0, alpha: 0.6)
        loadingIndicator.center = self.view.center;
        loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
        loadingIndicator.color = UIColor.whiteColor()
        loadingIndicator.startAnimating()
        self.view.addSubview(loadingIndicator)


    }

您可以将它放在另一个具有黑色背景颜色的视图中。像这样的东西。如果需要,您还可以在该视图中添加 'Loading...' 标签。

func showIndicatorView(){

    let loadingIndicator = UIActivityIndicatorView(frame: CGRectMake(0, 0, 50, 50))
    let backgroundView = UIView()

    backgroundView.layer.cornerRadius = 05
    backgroundView.clipsToBounds = true
    backgroundView.opaque = false
    backgroundView.backgroundColor = UIColor(white: 0.0, alpha: 0.6)

    loadingIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.Gray
    loadingIndicator.color = UIColor.whiteColor()
    loadingIndicator.startAnimating()

    let loadingLabel = UILabel()
    loadingLabel.text = "Loading..."
    let textSize: CGSize = loadingLabel.text!.sizeWithAttributes([NSFontAttributeName: loadingLabel.font ])

    loadingLabel.frame = CGRectMake(50, 0, textSize.width, textSize.height)
    loadingLabel.center.y = loadingIndicator.center.y

    backgroundView.frame = CGRectMake(0, 0, textSize.width + 70, 50)
    backgroundView.center = self.view.center;

    self.view.addSubview(backgroundView)
    backgroundView.addSubview(loadingIndicator)
    backgroundView.addSubview(loadingLabel)
}

使用 MBProgressHud 库。

这就是你需要的:-

https://github.com/jdg/MBProgressHUD