简体   繁体   中英

Activity indicator not showing in iOS 6

Inside my (void)applicationDidFinishLaunching:(UIApplication *)application method in my app's delegate, I have the following code to display an activity indicator:

spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.alpha = 1.0;
spinner.center = self.window.center;
spinner.layer.shadowColor = [UIColor grayColor].CGColor;
spinner.layer.shadowRadius = 1;
spinner.layer.shadowOpacity = 0.5;
spinner.layer.shadowOffset = CGSizeMake(0, 1);
[self.window addSubview:spinner];

The above code works fine in iOS 5 and earlier. However, the activity indicator doesn't display in iOS 6. How can I modify the above code so that the activity indicator is shown for iOS 6 as well?

Thanks!

try to add the last two lines to your code:

spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.alpha = 1.0;
spinner.center = self.window.center;
spinner.layer.shadowColor = [UIColor grayColor].CGColor;
spinner.layer.shadowRadius = 1;
spinner.layer.shadowOpacity = 0.5;
spinner.layer.shadowOffset = CGSizeMake(0, 1);
[self.window addSubview:spinner];


[spinner setHidden:YES];
[spinner startAnimating];

Keep in mind that no views will display in the UI until the applicationDidFinishLaunching:withOptions: method returns. You cannot set a UIActivityIndicatorView and expect it to show instantly. iOS will not update the UI until the current run loop finishes. Ostensibly your Default.png is being displayed during the execution of applicationDidFinishLaunching:withOptions: and your rootViewController will not take over the UI until that method is complete.

If you wish to show a UIActivityIndicatorView over your Default.png to indicate to the user that the app is starting, then you can do something like this:

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"Default.png"]];
spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
spinner.frame = CGRectMake(0.0, 0.0, 60.0, 60.0);
spinner.center = CGPointMake(imageView.center.x, imageView.center.y);

spinner.alpha = 1.0;
spinner.layer.shadowColor = [UIColor grayColor].CGColor;
spinner.layer.shadowRadius = 1;
spinner.layer.shadowOpacity = 0.5;
spinner.layer.shadowOffset = CGSizeMake(0, 1);
[spinner startAnimating];
[imageView addSubview:spinner];
[self.window addSubview:imageView];
[self.window makeKeyAndVisible];    

dispatch_async(dispatch_get_main_queue(), ^{
    // The rest of your method here
    // Be sure to remove the UIImageView from the window when the processing is complete.
}); 

This way the applicationDidFinishLaunching:withOptions: will return control back to iOS and the view will be displayed. Once you've loaded your rootViewController , you can remove the imageView from the window.

Also, it's not a good idea to add the UIActivityIndicatorView directly to the UIWindow , but rather you should add it to a pre-existing view that is already a subView of the window like I demonstrated above.

I hope this helps.

添加以下行:

[spinner performSelector:@selector(startAnimating) withObject:nil afterDelay:0.0f];

I had a similar problem when using a UIActivityIndicatorView in a UITableViewCell . The problem was that calling the startAnimating method on cell creation wasn't working.

To solve this I called the startAnimating method in the cellForRowAtIndexPath method.

This is probably the same with calling startAnimating in applicationDidFinishLaunching . You need to call this when the window is actually being displayed. A timer could work, or even better, create a UIViewController and put the spinner in that view.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM