在app启动时以模态方式呈现视图控制器 [英] Present view controller modally at app launch

查看:471
本文介绍了在app启动时以模态方式呈现视图控制器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有一个设置屏幕,如果满足某些条件,应该在根视图控制器上以模态方式显示。

My application has a setup screen that should be presented modally on the root view controller if certain conditions are met.

我在SO和互联网上查看了到目前为止最接近答案的方法是:

I have looked around on SO and the internet and the closest answer so far to how to go about doing this is here:

AppDelegate,rootViewController和presentViewController

这种方法存在两个问题:

There are 2 problems with this approach however:


  1. 在iOS 8中,这样做会使日志显示在控制台中,这似乎不是错误,但可能不是很好:

UITabBarController的开始/结束外观转换的不平衡调用:0x7fe20058d570。


  1. 当应用程序启动时,根视图控制器实际上会非常短暂地显示,然后淡入到呈现的视图控制器中(即使你是gh我明确地在我的 presentViewController 方法上调用 animated:NO

  1. The root view controller actually shows up very briefly when the app launches, and then fades into the presented view controller (even though I explicitly call animated:NO on my presentViewController method).

我知道我可以在 applicationDidFinishLaunchingWithOptions:中动态设置我的根控制器,但我特别希望以模态方式显示设置屏幕,所以当用户完成它时,它会解散并显示应用程序的真实第一个视图。这就是说,我不想动态地将我的根视图控制器更改为我的设置屏幕,并在用户完成设置时以模态方式呈现我的应用体验。

I understand that I can set my root controller dynamically in applicationDidFinishLaunchingWithOptions: but I specifically want to present the setup screen modally, so that when the user is done with it, it dismisses and the true first view of the application is revealed. This is to say, I don't want to dynamically change my root view controller to my setup screen, and present my app experience modally when the user is done setting up.

在我的根视图控制器上显示视图控制器 viewDidLoad 方法也会在第一次启动应用程序时导致UI明显闪烁。

Presenting the view controller on my root view controller viewDidLoad method also leads to a noticeable blink of the UI when the app is launched for the first time.

是否有可能在应用程序呈现任何内容之前以编程方式呈现视图控制器,以便第一个视图就位于模态视图控制器?

Is it possible to programmatically present a view controller modally, before the application has rendered anything so that the first view in place is the modal view controller?

更新:感谢您的评论,按建议添加我当前的代码:

UPDATE: Thank you for the comments, adding my current code as suggested:

在我的 AppDelegate .m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];

    [self.window makeKeyAndVisible];
    [self.window.rootViewController presentViewController:[storyboard instantiateViewControllerWithIdentifier:@"setupViewController"] animated:NO completion:NULL];

    return YES;
}

这是我需要的,除了它简要显示窗口的根当应用程序启动时,查看控制器一秒钟,然后淡化setupViewController,我觉得奇怪的是,我在没有动画的情况下呈现它并且褪色不是模态视图控制器的呈现方式。

This does what I need except for the fact that it briefly shows the window's root view controller for a second when the application launches, then fades the setupViewController, which I find odd given that I am presenting it without animation and fading is not how a modal view controller is presented anyway.

唯一能让我接近的是手动添加视图在根视图控制器的视图中执行加载方法如下:

The only thing that has gotten me close is manually adding the view in the root view controller's view did load method like so:

- (void)viewDidLoad
{
    [self.view addSubview:setupViewController.view];
    [self addChildViewController:setupViewController];
}

这种方法的问题在于我不能再原生地解雇setupViewController,现在需要处理视图层次结构并自己动画,如果它是唯一的解决方案,这很好,但是我希望在根视图控制器显示之前有一种在没有动画的情况下以模态方式添加视图控制器的制裁方法。

The problem with this approach is that I can no longer "natively" dismiss the setupViewController, and will now need to deal with the view hierarchy and animated it out myself, which is fine if it's the only solution, but I was hoping there was a sanctioned way of adding a view controller modally without animation before the root view controller displays.

更新2:在尝试了很多事情并等待2个月的答案之后,这个问题提出了最有创意的解决方案:

UPDATE 2: After trying a lot of things out and waiting for an answer for 2 months, this question proposes the most creative solution:

iOS在没有闪存的情况下启动模态视图控制器

我想是时候接受它是不可能的来呈现在根视图控制器出现之前以模态方式查看而不进行动画但是,该线程中的建议是创建启动屏幕的实例,并将其保持为超过默认值,直到模态视图控制器有机会出现。

I guess it's time to accept that it's just not possible to present a view modally without animation before the root view controller appears. However the suggestion in that thread is to create an instance of your Launch Screen and leave that on for longer than default until the modal view controller has had a chance to present itself.

推荐答案


我想是时候接受它是不可能的在根视图控制器出现之前在没有动画的情况下以模态方式呈现视图。

I guess it's time to accept that it's just not possible to present a view modally without animation before the root view controller appears.

出现之前,不,你不能出现。但是有多种有效的方法可以直观地解决这个问题。我推荐下面的解决方案A,因为它简单。

Before it appears, no, you can't present. But there are multiple valid approaches to solve this visually. I recommend solution A below for its simplicity.

此处显示解决方案由ullstrm 并且不会受到开始/结束外观转换的不平衡调用

let launchScreenView = UIStoryboard(name: "LaunchScreen", bundle: nil).instantiateInitialViewController()!.view!
launchScreenView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
launchScreenView.frame = window!.rootViewController!.view.bounds
window?.rootViewController?.view.addSubview(launchScreenView)
window?.makeKeyAndVisible()
// avoiding: Unbalanced calls to begin/end appearance transitions.
DispatchQueue.global().async {
    DispatchQueue.main.async {
        self.window?.rootViewController?.present(myViewControllerToPresent, animated: false, completion: {
            launchScreenView.removeFromSuperview()
        })
    }
}



B 。 addChildViewController首先,然后删除,然后出现



解决方案由Benedict Cohen提供

这篇关于在app启动时以模态方式呈现视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆