简体   繁体   English

活动指示器未在iOS 6中显示

[英]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: 在我的应用程序委托中的(void)applicationDidFinishLaunching:(UIApplication *)应用程序方法内部,我具有以下代码来显示活动指示器:

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. 上面的代码在iOS 5及更早版本中可以正常工作。 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? 但是,活动指示器没有在iOS 6中显示。如何修改上面的代码,以便活动指示器也能在iOS 6中显示?

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. 请记住,直到applicationDidFinishLaunching:withOptions:方法返回之前,UI中都不会显示任何视图。 You cannot set a UIActivityIndicatorView and expect it to show instantly. 您不能设置UIActivityIndicatorView并期望它立即显示。 iOS will not update the UI until the current run loop finishes. 直到当前运行的循环结束的iOS将不会更新UI。 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. 通常,在执行applicationDidFinishLaunching:withOptions:过程中将显示Default.png ,并且在该方法完成之前, rootViewController不会接管UI。

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: 如果您希望在Default.png上显示UIActivityIndicatorView来向用户指示应用程序正在启动,则可以执行以下操作:

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. 这样, applicationDidFinishLaunching:withOptions:会将控制权返回给iOS,并显示视图。 Once you've loaded your rootViewController , you can remove the imageView from the window. 一旦你载入你的rootViewController ,你可以删除imageView从窗口。

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. 另外,将UIActivityIndicatorView直接添加到UIWindow也不是一个好主意,而是应该将其添加到已经存在的窗口的SubView的现有视图中,就像我上面演示的那样。

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 . UITableViewCell使用UIActivityIndicatorView时,我遇到了类似的问题。 The problem was that calling the startAnimating method on cell creation wasn't working. 问题是在单元格创建上调用startAnimating方法无法正常工作。

To solve this I called the startAnimating method in the cellForRowAtIndexPath method. 为了解决这个问题,我在cellForRowAtIndexPath方法中调用了startAnimating方法。

This is probably the same with calling startAnimating in applicationDidFinishLaunching . 这与在applicationDidFinishLaunching调用startAnimating可能是相同的。 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. 计时器可以工作,甚至更好,它可以创建UIViewController并将微调器置于该视图中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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