如何在自定义视图中以编程方式添加 UIActivityIndi​​cator?

how to add UIActivityIndicator programmatically in a custom view?

我正在使用这个库 https://github.com/PiXeL16/RevealingSplashView 添加启动画面之类的东西。

我在从 Internet 获取数据时使用 RevealingSplashView。但不幸的是,如果请求时间太长,它会使应用程序像冻结一样。所以我想在屏幕的底部中心或 RevealingSplashView 的其他地方添加类似 UIActivityIndi​​catorView 的内容。

var revealingSplashView = RevealingSplashView(iconImage: UIImage(named: "loadscreenLogo")!,iconInitialSize: CGSize(width: 110, height: 110), backgroundColor: AppColor.mainYellow.getUIColor())
revealingSplashView.addSubview(UIActivityIndicatorView())

但是什么也没有显示

以下是我的做法。您需要一个 UIActivityIndi​​catorView 实例,而不是使用 class 本身。

//loading indicator
let indicator:UIActivityIndicatorView = UIActivityIndicatorView  (activityIndicatorStyle: UIActivityIndicatorViewStyle.gray)

然后加载指示器 - 它位于屏幕中央,但我们可以使用 .center CGPoint 轻松更改它。您可以使用 .frame 使指示器更大。

fileprivate func loading(){
    indicator.color = UIColor.gray
    indicator.frame = CGRect(x: 0.0, y: 0.0, width: 15.0, height: 15.0)
    indicator.center =  CGPoint(x: revealingSplashView center.x, y: revealingSplashView.center.y - (navigationController?.navigationBar.frame.height)! )
    revealingSplashView.addSubview(indicator)
    indicator.bringSubview(toFront: self.view)
    indicator.startAnimating()
}

完成后

self.indicator.stopAnimating()

并删除指标