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

前端 未结 15 895
盖世英雄少女心
盖世英雄少女心 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)
        }
    }
    
    0 讨论(0)
  • 2020-12-22 23:02

    Swift 4, Autolayout version

    func showActivityIndicator(on parentView: UIView) {
        let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: .gray)
        activityIndicator.startAnimating()
        activityIndicator.translatesAutoresizingMaskIntoConstraints = false
    
        parentView.addSubview(activityIndicator)
    
        NSLayoutConstraint.activate([
            activityIndicator.centerXAnchor.constraint(equalTo: parentView.centerXAnchor),
            activityIndicator.centerYAnchor.constraint(equalTo: parentView.centerYAnchor),
        ])
    }
    
    0 讨论(0)
  • 2020-12-22 23:03

    If you are using Swift, this is how you do it

    let activityView = UIActivityIndicatorView(activityIndicatorStyle: .whiteLarge)
    activityView.center = self.view.center
    activityView.startAnimating()
    
    self.view.addSubview(activityView)
    
    0 讨论(0)
  • 2020-12-22 23:03

    For Swift 3 you can use the following:

     func setupSpinner(){
        spinner = UIActivityIndicatorView(frame: CGRect(x: 0, y: 0, width: 40, height:40))
        spinner.color = UIColor(Colors.Accent)
        self.spinner.center = CGPoint(x:UIScreen.main.bounds.size.width / 2, y:UIScreen.main.bounds.size.height / 2)
        self.view.addSubview(spinner)
        spinner.hidesWhenStopped = true
    }
    
    0 讨论(0)
  • 2020-12-22 23:08

    You can set the position like this

            UIActivityIndicatorView *activityView = 
    [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    activityView.frame = CGRectMake(120, 230, 50, 50);
    [self.view addSubview:activityView];
    

    Accordingly change the frame size.....

    0 讨论(0)
  • 2020-12-22 23:08

    If you want to center the spinner using AutoLayout, do:

    UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    [activityView startAnimating];
    [self.view addSubview:activityView];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:activityView
                                                          attribute:NSLayoutAttributeCenterX
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeCenterX
                                                         multiplier:1.0
                                                           constant:0.0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:activityView
                                                          attribute:NSLayoutAttributeCenterY
                                                          relatedBy:NSLayoutRelationEqual
                                                             toItem:self.view
                                                          attribute:NSLayoutAttributeCenterY
                                                         multiplier:1.0
                                                           constant:0.0]];
    
    0 讨论(0)
提交回复
热议问题