从applicationDidEnterBackground中关闭一个视图并呈现一个新视图

3
我有一个应用程序,在用户启动应用程序或从后台重新打开应用程序时,会呈现自己的密码输入界面。当用户从后台重新打开应用程序时,不应该出现真实应用程序的“闪烁”,换句话说,安全屏幕需要在用户重新打开应用程序之前完全加载。
对于大多数屏幕,我已经很好地设置了这个功能。
在某种情况下,用户可以旋转应用程序,从选项卡栏控制器调用到水平视图控制器。在这种情况下,我遇到了一些问题。如果我不弹出旋转的屏幕,则输入屏幕会水平显示,即使用户在纵向重新打开应用程序也是如此。
如果我使用动画来解除它,则锁定屏幕直到应用程序重新启动才开始加载,因此您会看到内容的闪烁。
如果我不使用动画来解除它,则锁定屏幕仍会水平显示。
以下是从applicationDidEnterBackground中调用的内容:
    TabBarController *tbc = (TabBarController*)self.window.rootViewController;        

    void (^openPasscode)() = ^void() {
        KVPasscodeViewController *passcodeController = [[KVPasscodeViewController alloc] init];
        passcodeController.delegate = self;
        UINavigationController *passcodeNavigationController = [[UINavigationController alloc] initWithRootViewController:passcodeController];

        // Change animated to YES and the new view isn't loaded until after the app restarts
        [self.window.rootViewController presentViewController:passcodeNavigationController animated:NO completion:nil];

    };

    if (tbc.isShowingLandscapeView) {
        [self.window.rootViewController dismissViewControllerAnimated:NO completion:openPasscode];
    } else {
        openPasscode();
    }

不知道在 applicationWillEnterBackground: 中呈现您的密码屏幕后发出 [CATransaction flush] 是否有效? - nielsbot
3个回答

1
另一种可能性是,由于applicationDidEnterBackground在主线程上调用,因此在其中对视图进行的任何操作都不会在下一次通过主运行循环之前可见(这只有在应用程序返回到前台时才会发生——这就是为什么在安全视图出现在其上方之前看到主屏幕闪烁的原因)。
在重新排列视图之后,如果在离开之前强制应用程序再旋转一次主运行循环会怎样呢?您可以尝试在applicationDidEnterBackground结束之前添加以下内容:
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate date]];

这只是一个猜测 - 我没有尝试过,但我希望它能给你另一个探索方向。


1

尝试对持有密码视图控制器的UINavigationController进行子类化,并实现这些方法(来自UIViewController.h):

- (BOOL)shouldAutorotate {
    return YES; // when app launched while device in landscape. it needs to rotate to portrait.
}

- (NSUInteger)supportedInterfaceOrientations {
    return UIInterfaceOrientationMaskPortrait;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (UIInterfaceOrientationPortrait == interfaceOrientation);
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

我以为我已经尝试过这个了,但可能错过了某个方法或犯了一个错误。谢谢你的帮助。 - lewis

0

如果在 applicationWillEnterForeground 中呈现密码视图而不是在 applicationDidEnterBackground 中呈现,会发生什么?

我认为当应用程序恢复时,在主应用程序屏幕出现之前,它将呈现您的锁定屏幕视图控制器 - 这将避免任何主视图的“闪烁”,并且旋转中所做的任何更改都已经在视图出现之前解决。


我已经尝试过这个了,抱歉之前没在问题中提到。 - lewis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接