如何以编程方式放置activityIndi​​cator?

时间:2018-03-06 12:38:21

标签: swift swift3 uiactivityindicatorview activity-indicator

我想以编程方式将activityIndi​​cator放在我想要的位置,但我不知道如何。

我知道如何把它放在屏幕的中央:

activityIndicator.center = self.view.center

但我想要这样的事情:

activityIndicator.activityIndicator(frame: CGRect(x: 100, y: 100, width: 100, height: 50))

但我似乎无法使其发挥作用。

3 个答案:

答案 0 :(得分:4)

请将container视图的原点设置为在屏幕上的任何位置放置activityindicator。

    func showActivityIndicatory(uiView: UIView) {
        var container: UIView = UIView()
        container.frame = CGRect(x: 0, y: 0, width: 80, height: 80) // Set X and Y whatever you want
        container.backgroundColor = .clear

        let activityView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
        activityView.center = self.view.center
        activityView.startAnimating()


        container.addSubview(activityView)
        self.view.addSubview(container)
        activityView.startAnimating()
    }

答案 1 :(得分:0)

迅速5:

let activityIndicator = UIActivityIndicatorView(style: UIActivityIndicatorView.Style.gray)

    // Place the activity indicator on the center of your current screen
            myActivityIndicator.center = view.center

    // In most cases this will be set to true, so the indicator hides when it stops spinning
            myActivityIndicator.hidesWhenStopped = true

    // Start the activity indicator and place it onto your view
            myActivityIndicator.startAnimating()
            view.addSubview(myActivityIndicator)

    // Do something here, for example fetch the data from API


    // Finally after the job above is done, stop the activity indicator
            myActivityIndicator.stopAnimating()

答案 2 :(得分:0)

您可以声明:

var activityView: UIActivityIndicatorView?

然后,在您的类中,创建以下用于显示或隐藏指标的方法:

func showActivityIndicator() {
    activityView = UIActivityIndicatorView(style: .whiteLarge)
    activityView?.center = self.view.center
    self.view.addSubview(activityView!)
    activityView?.startAnimating()
}

func hideActivityIndicator(){
    if (activityView != nil){
        activityView?.stopAnimating()
    }
}

此代码对我有用。祝你好运!