当我杀死我的iPhone应用程序并重新启动它时,我有一个应用程序的先前状态的图像而不是启动图像。如何强制iOS始终显示启动图像?
要强制iOS使用其默认图像启动应用程序,请调用[[UIApplication sharedApplication] ignoreSnapshotOnNextApplicationLaunch];
以实现状态保留。
当您的应用重新启动时,系统会显示此快照图像以代替您应用的默认启动图像,以保留您的应用仍在运行的概念。如果您认为在重新启动应用程序时快照无法正确反映应用程序的用户界面,则可以调用此方法以防止拍摄快照图像。如果您这样做,UIKit会使用您应用的默认启动图像。
另请参阅this question了解更多详情。
如果在状态恢复期间未调用应用程序,则不会调用ignoreSnapshotOnNextApplicationLaunch方法。
如果您的应用不支持多任务功能,其Info.plist中的UIApplicationExitsOnSuspend键设置为YES。建议的解决方案不起作用。
对我来说,我使用下面的代码:
applicationWillResignActive用于应用程序运行时的情况,双击主页按钮调用多任务托盘。启动画面将显示在应用程序屏幕上。它完美地运作。
applicationWillTerminate每次都不起作用,因为在某些阶段永远不会调用该函数
- (void)applicationWillResignActive:(UIApplication *)application {
NSLog(@"Application Did Resign Active");
self.viewController.webView.hidden = YES;
NSString *splashImage;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
splashImage = @"Default-Portrait~ipad.png";
}
else {
splashImage = @"Default~iphone.png";
}
UIImageView *splash = [[UIImageView alloc]initWithFrame:[self.window frame]];
[splash setImage:[UIImage imageNamed:splashImage]];
[self.window addSubview:splash];
}
- (void)applicationWillTerminate:(UIApplication *)application {
NSLog(@"Application is Terminated");
self.viewController.webView.hidden = YES;
NSString *splashImage;
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
splashImage = @"Default-Portrait~ipad.png";
}
else {
splashImage = @"Default~iphone.png";
}
UIImageView *splash = [[UIImageView alloc]initWithFrame:[self.window frame]];
[splash setImage:[UIImage imageNamed:splashImage]];
[self.window addSubview:splash];
}
Please follow this -
-(void)applicationWillResignActive:(UIApplication *)application
{
imageView = [[UIImageView alloc]initWithFrame:[self.window frame]];
[imageView setImage:[UIImage imageNamed:@"Portrait(768x1024).png"]];
[self.window addSubview:imageView];
}
Here to remove your imageview:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
if(imageView != nil) {
[imageView removeFromSuperview];
imageView = nil;
}
}
It is working and tested many times.
Swift 4.2
func applicationDidEnterBackground(_ application: UIApplication) {
let imageView = UIImageView(frame: self.window!.bounds)
imageView.tag = 25
imageView.image = UIImage(named: <YOUR_SPLASH_SCREEN_IMG>)
UIApplication.shared.keyWindow?.subviews.last?.addSubview(imageView)
}
func applicationWillEnterForeground(_ application: UIApplication) {
if let imageView : UIImageView = UIApplication.shared.keyWindow?.subviews.last?.viewWithTag(25) as? UIImageView {
imageView.removeFromSuperview()
}
}
对于我目前的项目,我想在应用程序被杀后强制重新启动。我将此添加到应用程序的plist中:
为确保启动画面正确,我使用了以上建议:
- (void)applicationWillResignActive:(UIApplication *)application {
[[UIApplication sharedApplication] ignoreSnapshotOnNextApplicationLaunch]; }
和
- (void)applicationDidEnterBackground:(UIApplication *)application {
[[UIApplication sharedApplication] ignoreSnapshotOnNextApplicationLaunch]; }
它运作良好。