有些东西很简单,但是我还是把它记录了下来。
使用Storyboard创建一个新的项目后,应用程序从闪屏,到主窗口,再到第一个界面经过的文件分别是:LaunchScreen.storyboard->Main.storyboard->ViewController。
如果我们要纯代码构建一个应用程序,即上述步骤我们通过代码来控制的话,需要删除Main.storyboard,并修改相关配置,添加一些代码。
LaunchScreen.storyboard作为对闪屏的处理,这个不需要删除,系统提供的闪屏解决方案使用这个文件处理即可。
- 删除Main.storyboard
delete -> Move to Trash,直接删除Main.storyboard文件。
- 修改Main Interface配置
TARGETS->General->Development Info->Main Interface
清空Main Interface内容。
-
修改info.plist配置
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200511104456175.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L01vcnJpc18=,size_16,color_FFFFFF,t_70)
删除Storyboard Name这一项。
-
添加代码构应用程序窗口
SceneDelegate作为管理应用程序界面的类,在这个类里面我们需要创建一个主窗口(Window),用来显示应用程序
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// 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).
self.window = UIWindow(windowScene: scene as! UIWindowScene);
self.window?.backgroundColor = UIColor.white;
let rootViewController = ViewController();
self.window?.rootViewController = rootViewController;
self.window?.makeKeyAndVisible();
guard let _ = (scene as? UIWindowScene) else { return }
}