ios - 在应用程序启动时以模态方式呈现 View Controller

标签 ios objective-c

我的应用程序有一个设置屏幕,如果满足某些条件,它应该以模式方式呈现在 Root View Controller 上。

我已经在 SO 和互联网上四处查看,到目前为止,关于如何执行此操作的最接近答案是:

AppDelegate, rootViewController and presentViewController

但是这种方法有两个问题:

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

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

  1. Root View Controller 实际上在应用程序启动时非常短暂地出现,然后淡入呈现的 View Controller (即使我在我的 presentViewController 上显式调用了 animated:NO > 方法)。

我知道我可以在 applicationDidFinishLaunchingWithOptions: 中动态设置我的根 Controller ,但我特别想以模态方式呈现设置屏幕,这样当用户完成它时,它就会消失,真正的第一个显示应用程序的 View 。也就是说,我不想将我的 Root View Controller 动态更改为我的设置屏幕,并不想在用户完成设置时以模态方式呈现我的应用程序体验。

在我的 Root View Controller viewDidLoad 方法上呈现 View Controller 也会导致应用程序首次启动时 UI 明显闪烁。

是否可以在应用程序呈现任何内容之前以编程方式以模态方式呈现 View Controller ,以便第一个就位的 View 是模态视图 Controller ?

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

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

除了在应用程序启动时它短暂显示窗口的 Root View Controller 一秒钟,然后淡出 setupViewController 之外,这完成了我需要的事情,我觉得这很奇怪,因为我在没有动画的情况下呈现它,而淡出不是模态视图 Controller 是如何呈现的。

唯一让我接近的是在 Root View Controller 的 View 中手动添加 View ,加载方法如下:

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

这种方法的问题是我不能再“ native ”关闭 setupViewController,现在需要处理 View 层次结构并自己制作动画,如果这是唯一的解决方案,那很好,但我希望在 Root View Controller 显示之前,有一种在没有动画的情况下模态添加 View Controller 的认可方法。

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

iOS Present modal view controller on startup without flash

我想是时候接受根本不可能在 Root View Controller 出现之前以模态方式呈现没有动画的 View 了。但是,该线程中的建议是创建启动屏幕的一个实例,并将其保持打开状态的时间比默认时间长,直到模态视图 Controller 有机会展示自己。

最佳答案

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。

A.将 launchScreen 添加为 subview ,然后呈现,然后删除 launchscreen

Solution is presented here by 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.先添加ChildViewController,再移除,再呈现

Solution is presented here by Benedict Cohen .

关于ios - 在应用程序启动时以模态方式呈现 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28247709/

相关文章:

iOS swift JSON 到类对象

ios - 使用侧面菜单更改 Root View Controller

ios - Xcode 7.3 : Can't import MetalKit

ios - 使用 AVPlayer 时防止背景音频静音

objective-c - 如何从给定位置获取 NSView

ios - 无法注册 UITableViewCell 的自定义子类

ios - 如何从 iOS 中的 CFStream 获取远程地址?

ios - 如何解除分配从 Swift 结构引用的 UnsafeMutablePointer

ios - 以编程方式创建 TableView 时未显示

iPhone SDK : Redirecting stderr/stdout to XCode console