IOS使用纯代码布局替换掉默认的storyboard

在iOS11中,新增了SceneDelegate用来管理UI界面。之前在AppDelegate中操作window的代码应该都失效了。

在11+中,都需要在SceneDelegate来管理。在scene函数中。有一大段注释

// Use this method to optionally configure and attach the UIWindow window to the provided UIWindowScene scene.

// If using a storyboard, the window property will automatically be initialized and attached to the scene.

// This delegate does not imply the connecting scene or session are new (see application:configurationForConnectingSceneSession instead).

于是在这里位置,加上自己的UIWindow管理


 let windScene = scene as! UIWindowScene;
        self.window = UIWindow.init(windowScene: windScene)
        self.window?.rootViewController = ViewController();
        self.window?.makeKeyAndVisible()

个人觉得这里不需要用guard,直接转换UIWindowScene最好。

原文地址:https://www.cnblogs.com/boxrice/p/15725698.html