XCode 11 删除 main.storyboard (保留SceneDelegate))

问题

在 XCode 11 中,新建一个项目,默认会创建 main.storyboard 文件,如果我们想要手写布局和 VC,就需要删除 storyboard。网上搜了一些现有的方案,都是直接连 SceneDelegate 都删除了,此处记录一种保留 SceneDelegate 的方案。

解决方案

分为三步

  1. 删除 storyboard 文件
  2. 删除配置项
  3. 创建 Window 和 VC

删除 storyboard

这步很简单,直接删除即可。

删除配置项

在 info.plist 中,删除两个项目:

  1. Main storyboard file base name
  2. Application Scene Manifest -> Scene Configuration -> Application Scene Role -> Item 0 -> Storyboard name

创建 Window 和 VC

修改 SceneDelegate 的 willConnectTo 方法

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).
    guard let windowScene = (scene as? UIWindowScene) else { return }
        
    let window = UIWindow(windowScene: windowScene)
    let nav = UINavigationController()
    nav.pushViewController(HomeViewController(), animated: false)
    window.rootViewController = nav
    self.window = window
    window.makeKeyAndVisible()
}

其中,rootViewController 根据自己的需要指定。

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容