Activity indicator until load the next view

3.8k views Asked by At

In my firstViewController there is a UIButton (GalleryButton) and in my secondViewController there is a UITableView. When user taps the GalleryButton it takes 2-3 seconds time to open the secondViewController and load the images. I want to show an UIActivityIndicator until load the secondViewController. How to do so??

3

There are 3 answers

2
Remy Cilia On BEST ANSWER

You should load the images in a background thread and display the UIActivityIndicator in the main thread. I already replied to a similar issue here: https://stackoverflow.com/a/41529056/1370336

// Main thread by default: 
// show progress bar here.

DispatchQueue.global(qos: .background).async {
    // Background thread:
    // start loading your images here

    DispatchQueue.main.async {
        // Main thread, called after the previous code:
        // hide your progress bar here
    }
}
0
Sachin Dobariya On

Create Activity Indicator Programetically in your Second View Controller

    var activityIndicator = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.Gray)

Add Below Code in viewDidLoad() of Second View Contoller

   activityIndicator.hidesWhenStopped = true
   activityIndicator.center = view.center
   activityIndicator.startAnimating() //For Start Activity Indicator

When Data is Filled in Table View Completely than Add below code for stoping Activity Indicator

   activityIndicator.stopAnimating() //For Stop Activity Indicator
0
Nimrod Borochov On

this worked for me

#import "ViewController.h"
#import "NextVC.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *aiStart;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.aiStart.hidden = YES;
}

- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    self.aiStart.hidden = YES;
    [self.aiStart stopAnimating];
}

- (IBAction)btnShowNextVCTapped:(id)sender {
    dispatch_async(dispatch_get_main_queue(), ^{

        self.aiStart.alpha = 0;
        self.aiStart.hidden = NO;
        [self.aiStart startAnimating];

        [UIView animateWithDuration:0.3 animations:^{
            self.aiStart.alpha = 1;
        } completion:^(BOOL finished) {
            NextVC* nextVC = [self.storyboard instantiateViewControllerWithIdentifier:@"NextVC"];

            [self presentViewController:nextVC animated:YES completion:nil];
        }];
    });


}