去除Storyboard,改用纯代码

Xcode: Version 13.0 (13A233)
Swift: Version 5.5

创建新的项目后,项目默认使用的是Storyboard,现在想改用纯代码的方式

1.删除 Main Interface

路径: TARGETS -> General
Main Interface 中的值删除

2.删除 Storyboard Name

路径:Info -> Scene Configuration -> Application Session Role -> Storyboard Name

删除 Storyboard Name 这一项(点击减号删除)

3.代码创建根控制器

路径:SceneDelegate

SceneDelegate 中的

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions)

编写代码创建方式

func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
    
    guard let _ = (scene as? UIWindowScene) else { return }
    
    guard let windowSence = (scene as? UIWindowScene) else { return }
    
    window = UIWindow(frame: windowSence.coordinateSpace.bounds)
    
    window?.windowScene = windowSence
    
    let navigationController = UINavigationController(rootViewController: NewsViewController())
    navigationController.navigationBar.prefersLargeTitles = true
    
    window?.rootViewController = navigationController
    window?.makeKeyAndVisible()
}
posted @ 2021-11-02 10:53  小小个子大个头  阅读(116)  评论(0编辑  收藏  举报