微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

ios – 在app从背景返回之前,applicationDidEnterBackground中添加的视图不可见

我在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];
}

解决方法

当applicationDidEnterBackground方法返回时,应用程序的当前视图的快照将用于多任务视图.您没有看到更新的原因是因为快照是在绘制周期之前拍摄的.即使您调用setNeedsdisplay,仍然会拍摄快照.

要等到视图更新(添加子视图后),请调用

[self.view snapshotViewAfterScreenUpdates:YES];

值为YES.这会强制您首先进行渲染.你可以在这里阅读苹果文档中有关处理这类内容的更多信息:https://developer.apple.com/library/ios/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/StrategiesforHandlingAppStateTransitions/StrategiesforHandlingAppStateTransitions.html#//apple_ref/doc/uid/TP40007072-CH8-SW27

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐