我想在应用程序从后台恢复时在我的应用程序上添加启动画面。这可能吗?提前致谢
答案 0 :(得分:6)
您可以在-[UIApplicationDelegate applicationWillResignActive:]
更新视图堆栈。
当应用程序恢复时,更改将会显示,您可以在-[UIApplicationDelegate applicationDidBecomeActive:]
中再次删除初始屏幕。
答案 1 :(得分:3)
一些代码与Morten的答案一致。我还想注意,这在模拟器中表现不正常,但是当你在设备上运行它时。模拟器显示黑屏,直到调用removeFromSuperview
。
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// We don't want to show a splash screen if the application is in UIApplicationStateInactive (lock/power button press)
if (application.applicationState == UIApplicationStateBackground) {
UIImageView *splash = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"splashimage.png"]];
splash.frame = self.window.bounds;
[self.window addSubview:splash];
}
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Make sure you do not remove the last view in the event something odd happens
if ([[self.window subviews] count] > 1) {
// Not recommended by Apple, but client gets what client wants
[NSThread sleepForTimeInterval:1.0];
[[[self.window subviews] lastObject] removeFromSuperview];
}
}
注意,我使用了applicationDidEnterBackground而不是applicationWillResignActive,因为applicationState有助于区分“按下的主页按钮”和“按下的锁定/电源按钮”。 UIApplicationStateBackground =“按下主页按钮”。