How to display activity indicator in middle of the iphone screen?

前端 未结 15 1094
盖世英雄少女心
盖世英雄少女心 2020-12-22 22:43

When we draw the image, we want to display the activity indicator. Can anyone help us?

15条回答
  •  感动是毒
    2020-12-22 22:57

    Swift 3, xcode 8.1

    You can use small extension to place UIActivityIndicatorView in the centre of UIView and inherited UIView classes:

    Code

    extension UIActivityIndicatorView {
    
        convenience init(activityIndicatorStyle: UIActivityIndicatorViewStyle, color: UIColor, placeInTheCenterOf parentView: UIView) {
            self.init(activityIndicatorStyle: activityIndicatorStyle)
            center = parentView.center
            self.color = color
            parentView.addSubview(self)
        }
    }
    

    How to use

    let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge, color: .gray,  placeInTheCenterOf: view)
    activityIndicator.startAnimating()
    

    Full Example

    In this example UIActivityIndicatorView placed in the centre of the ViewControllers view:

    import UIKit
    
    class ViewController: UIViewController {
    
        override func viewDidLoad() {
            super.viewDidLoad()
            // Do any additional setup after loading the view, typically from a nib.
            let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge, color: .gray,  placeInTheCenterOf: view)
            activityIndicator.startAnimating()
        }
    }
    
    extension UIActivityIndicatorView {
    
        convenience init(activityIndicatorStyle: UIActivityIndicatorViewStyle, color: UIColor, placeInTheCenterOf parentView: UIView) {
            self.init(activityIndicatorStyle: activityIndicatorStyle)
            center = parentView.center
            self.color = color
            parentView.addSubview(self)
        }
    }
    

提交回复
热议问题