【发布时间】:2015-06-05 12:46:41
【问题描述】:
我在 applicationDidEnterBackground 的应用程序主窗口中添加了一个 imageView:
- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIImageView *splash = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"splashimage.png"]];
splash.frame = self.window.bounds;
[self.window addSubview:splash];
}
我希望当我通过按下设备的主页按钮将应用程序置于后台,然后通过双击主页按钮查看任务管理器时,我会看到 splashimage.png 显示。但似乎应用程序进入后台时截取的屏幕截图不包括此叠加图像视图。我以为会的,因为我添加了没有动画的 imageView。
为什么会发生这种情况?
更新:即使我在 applicationDidEnterBackground 中隐藏我的窗口,在应用程序进入后台状态后,我仍然可以在任务管理器中看到未隐藏的完整窗口。应用程序从后台返回后,当我按下主页按钮时,该窗口才会隐藏。
- (void)applicationDidEnterBackground:(UIApplication *)application
{
//this does not work either!
self.window.hidden = YES;
}
更新:顺便说一下,我确实理解 applicationDidEnterBackground 有 5 秒的时间来完成。我现在只用这个测试
self.window.hidden = YES;
在applicationDidEnterBackground中和applicationWillResignActive中什么都没有,并且当应用进入后台时窗口仍然没有隐藏;只有当它返回到前台时。所以这告诉我,在我的应用程序的某个地方,一定有其他东西不允许在 applicationDidEnterBackground 中发生这种情况。顺便说一句,如果我移动了
self.window.hidden = YES;
对于 applicationWillResignActive,当应用程序进入后台时窗口被隐藏。但我试图弄清楚什么会阻止应用程序完成 applicationDidEnterBackground 中的这个单一、简单、非动画的任务。任何想法表示赞赏。
更新:此特定问题与使用 BannerViewController (iAd) 有关。我在 UITabBarController 中使用它。尚不确定究竟是什么问题,或者它是否也与它在 UITabBarController 中的使用有关。
更新:我认为该问题与 UITabBarController 无关,但通常与 BannerViewController (iAd) 有关。现在明白为什么...
更新:BannerViewController.m 中的这一行导致了问题:
[self.view addSubview:bannerView]
在这个方法中:
- (void)viewDidLayoutSubviews
{
CGRect contentFrame = self.view.bounds, bannerFrame = CGRectZero;
ADBannerView *bannerView = [BannerViewManager sharedInstance].bannerView;
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
NSString *contentSizeIdentifier;
// If configured to support iOS <6.0, then we need to set the currentContentSizeIdentifier in order to resize the banner properly.
// This continues to work on iOS 6.0, so we won't need to do anything further to resize the banner.
if (contentFrame.size.width < contentFrame.size.height) {
contentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
} else {
contentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
}
bannerFrame.size = [ADBannerView sizeFromBannerContentSizeIdentifier:contentSizeIdentifier];
#else
// If configured to support iOS >= 6.0 only, then we want to avoid currentContentSizeIdentifier as it is deprecated.
// Fortunately all we need to do is ask the banner for a size that fits into the layout area we are using.
// At this point in this method contentFrame=self.view.bounds, so we'll use that size for the layout.
bannerFrame.size = [_bannerView sizeThatFits:contentFrame.size];
#endif
if (bannerView.bannerLoaded) {
contentFrame.size.height -= bannerFrame.size.height;
bannerFrame.origin.y = contentFrame.size.height;
} else {
bannerFrame.origin.y = contentFrame.size.height;
}
_contentController.view.frame = contentFrame;
// We only want to modify the banner view itself if this view controller is actually visible to the user.
// This prevents us from modifying it while it is being displayed elsewhere.
if (self.isViewLoaded && (self.view.window != nil)) {
[self.view addSubview:bannerView];
bannerView.frame = bannerFrame;
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_6_0
bannerView.currentContentSizeIdentifier = contentSizeIdentifier;
#endif
}
}
不完全确定为什么或什么,如果我仍然想使用 BannerViewController,是否可以做任何事情来解决它。
更新:从下面接受的答案,这是为我解决这个问题的方法(bannerVC 是对 BannerViewController 的引用)。
- (void)applicationWillResignActive:(UIApplication *)application {
[bannerVC.view snapshotViewAfterScreenUpdates:YES];
}
更新:我意识到这段代码确实应该在 applicationDidEnterBackground 中。但是,使用 AdBannerView 似乎无法及时停止动画,以便发生这种情况。所以现在,据我了解,applicationWillResignActive 是剩下的,但它给用户留下了“低于”的体验。我将不胜感激有关如何停止 AdBannerView 动画的任何建议,以便快照可以显示在 applicationDidEnterBackground 中,而不仅仅是从后台返回时。
更新: 复制问题:
- 下载iAdSuite
- 打开 TabbedBanner 示例。
- 将以下代码添加到 AppDelegate:
- 运行应用程序。 (框架因为尚未更新而混乱,但此示例仍会显示问题)
- 点按一次主页按钮可将应用置于后台。
- 双击主页按钮可在任务管理器中查看该应用。
如果您将 applicationWillResignActive 中的代码保留为注释,而 applicationDidEnterBackground 中的代码未注释,您将不会在任务管理器中看到蓝色的闪屏。但是,如果您在 applicationDidEnterBackground 中注释了代码并取消注释 applicationWillResignActive 中的代码,您应该会在任务管理器中看到蓝色的闪屏。然而,这是不可取的。问题是如何在 applicationDidEnterBackground 方法中显示启动画面。
- (void)applicationWillResignActive:(UIApplication *)application {
//works - but undesirable
//[self addDummyView];
//[_tabBarController.view snapshotViewAfterScreenUpdates:YES];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
//does not work - needs to work to show dummyview only when application actually goes into the background
[self addDummyView];
[_tabBarController.view snapshotViewAfterScreenUpdates:YES];
}
- (void)addDummyView {
UIView *topView = _tabBarController.view;
UIView *colorView = [[UIView alloc] initWithFrame:topView.frame];
[colorView setBackgroundColor:[UIColor blueColor]];
[topView addSubview:colorView];
[topView bringSubviewToFront:colorView];
}
【问题讨论】:
-
但我的回答被贬低了:(
标签: ios user-interface window lifecycle multitasking