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

前端 未结 15 896
盖世英雄少女心
盖世英雄少女心 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 23:10

    This is the correct way:

    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    activityIndicator.center = CGPointMake(self.view.frame.size.width / 2.0, self.view.frame.size.height / 2.0);
    [self.view addSubview: activityIndicator];
    
    [activityIndicator startAnimating];
    
    [activityIndicator release];
    

    Setup your autoresizing mask if you support rotation. Cheers!!!

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

    Try this way

    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
        activityIndicator.frame = CGRectMake(10.0, 0.0, 40.0, 40.0);
        activityIndicator.center = super_view.center;
        [super_view addSubview: activityIndicator];
    
    [activityIndicator startAnimating];
    
    0 讨论(0)
  • 2020-12-22 23:12

    Code updated for Swift 3

    Swift code

    import UIKit
    class ViewController: UIViewController {
    
        let activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
    
        @IBOutlet weak var myBlueSubview: UIView!
    
        @IBAction func showButtonTapped(sender: UIButton) {
            activityIndicator.startAnimating()
        }
    
        @IBAction func hideButtonTapped(sender: UIButton) {
            activityIndicator.stopAnimating()
        }
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            // set up activity indicator
            activityIndicator.center = CGPoint(x: myBlueSubview.bounds.size.width/2, y: myBlueSubview.bounds.size.height/2)
            activityIndicator.color = UIColor.yellow
            myBlueSubview.addSubview(activityIndicator)
        }
    }
    

    Notes

    • You can center the activity indicator in middle of the screen by adding it as a subview to the root view rather than myBlueSubview.

      activityIndicator.center =  CGPoint(x: self.view.bounds.size.width/2, y: self.view.bounds.size.height/2)
      self.view.addSubview(activityIndicator)
      
    • You can also create the UIActivityIndicatorView in the Interface Builder. Just drag an Activity Indicator View onto the storyboard and set the properties. Be sure to check Hides When Stopped. Use an outlet to call startAnimating() and stopAnimating(). See this tutorial.

    • Remember that the default color of the LargeWhite style is white. So if you have a white background you can't see it, just change the color to black or whatever other color you want.

      activityIndicator.color = UIColor.black
      
    • A common use case would be while a background task runs. Here is an example:

      // start animating before the background task starts
      activityIndicator.startAnimating()
      
      // background task
      DispatchQueue.global(qos: .userInitiated).async {
      
          doSomethingThatTakesALongTime()
      
          // return to the main thread
          DispatchQueue.main.async {
      
              // stop animating now that background task is finished
              self.activityIndicator.stopAnimating()
          }
      }
      
    0 讨论(0)
提交回复
热议问题