Anjali
Anjali

Reputation: 1691

UIActivityIndicator doesn't stop in iOS

I am showing spinner(UIActivityIndicator) while download is going on. It shows spinner as download starts, but it doesn't stop showing once download is finished. i have seen some question for the same issue but did not help. I need to make it work in ios7 as well as iOS8.

I am using below code...

      - (void) startSpinner:(NSString *)message  {

    container = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    container.center = self.view.center;
    container.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.4];
    container.layer.cornerRadius = 10;
    container.layer.masksToBounds = true;
    container.tag = 134;
    activityLabel = [[UILabel alloc] init];
    activityLabel.frame = CGRectMake(0, 70, 100, 30);
    activityLabel.text = message;
    activityLabel.textColor = [UIColor darkGrayColor];
    activityLabel.textAlignment = NSTextAlignmentCenter;
    activityLabel.font = [UIFont boldSystemFontOfSize:10];
    [container addSubview:activityLabel];


    activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
    activityIndicator.center = CGPointMake(container.frame.size.width/2, container.frame.size.height/2);
    [container addSubview:activityIndicator];

    [self.view addSubview:container];

    [activityIndicator startAnimating];
}

-(void)stopSpinner{
    [activityIndicator stopAnimating];
    container = [self.view viewWithTag:134];
    [container removeFromSuperview];
    activityIndicator = nil;
    NSLog(@"activity indicator should be removed %@",activityIndicator);
}

And one more issue it does not show spinner in the center of the device screen.

Any help will be appreciated....

enter image description here

Upvotes: 0

Views: 141

Answers (2)

Shardul
Shardul

Reputation: 4274

You can add the spinner view on navigation controller, so it will look at the center as you want. I think this will solve your problem.

[self.navigationController.view addSubview:coverView];

And don't forget to remove it from self.navigationController.view

Upvotes: 1

Aurast
Aurast

Reputation: 3678

There are a few things wrong here.

container.center = self.view.center;

If you are trying to center the container in self.view, then this won't do it. self.view.center is the center of the view's frame but you would need to use the center of the view's bounds. Here is the corrected code:

container.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds));

You also need to change how you set the activity indicator's frame:

activityIndicator.center = CGPointMake(CGRectGetMidX(container.bounds), CGRectGetMidY(container.bounds));

Also, if you call startSpinner more than once, you can have more than one spinner added to the view, which could possibly be the reason your spinner doesn't seem to be stopping. I recommend that you alloc and init only one spinner and do it in the class constructor. You can do the same with the container and activityLabel objects. Alternatively, you could call stopSpinner at the top of startSpinner (and add some null-checks in stopSpinner to avoid referencing a null pointer).

It's impossible to say for sure based on your code why it appears that the spinner doesn't stop, but my guess is that you're calling startSpinner several times in a row without any intervening call to stopSpinner.

If you're only calling stopSpinner once then verify that you're doing so in the main thread.

Upvotes: 2

Related Questions