IOS开发日志-ios新建项目后-将storyboard去掉,版本调整为IOS13以下

目标IOS版本12,Xcode 16.2,记录ios新建项目后,如何将故事板storyboard和SceneDelegate 文件去掉,调整为代码开发。

创建后的项目中包含SceneDelegate,SceneDelegate 是 iOS 13 引入的类,用于管理应用程序的生命周期和界面显示。如果开发IOS13以下的项目,需要对SceneDelegate 进行调整。

调整步骤:

将 var window: UIWindow? 复制或者移动到 AppDelegate 当中。

删除 SceneDelegate

删除AppDelegate 中标红的代码。

删除 Main.storyboard 。

删除 Info.plist 中的标红部分。

设置默认显示的界面

AppDelegate 中添加如下代码。

Swift 复制代码
        // 设置默认显示页面(此处为固定的写法)
        let controller = ViewController()
        self.window = UIWindow(frame: UIScreen.main.bounds)
        window!.rootViewController = controller
        window!.makeKeyAndVisible()

程序运行后,没有显示红色背景,并且出现了报错,说明没有加载到 ViewController ,检查一下。

Thread 1: "Could not find a storyboard named 'Main' in bundle NSBundle </Users/dengming/Library/Developer/CoreSimulator/Devices/E911900E-95D7-40F2-BDA0-F7EDCF15F82E/data/Containers/Bundle/Application/D17467AA-3EA9-4577-8B0B-9C16FC6941D9/cmMusic.app> (loaded)"

将target中info 中的 Main storyboard file base name 属性值,设置为空。

再次运行,已经可以成功加载到 ViewController ,并移除了故事板。

实现效果

附:AppDelegate 和 ViewController 代码

Swift 复制代码
//
//  ViewController.swift
//  cmMusic
//
//  Created by deng ming on 2025/2/4.
//
import UIKit
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.backgroundColor = .red     
       // self.view.backgroundColor = .red
    }
}
//
//  ViewController.swift
//  cmMusic
//
//  Created by deng ming on 2025/2/4.
//
import UIKit
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.backgroundColor = .red
       // self.view.backgroundColor = .red
    }
}
相关推荐
茶底世界之下2 天前
蓝调鱼市的一盏暖灯:用 Awaking 把冷海与灯光分开
ios·app·产品
独隅2 天前
WebStorm macOS 安装部署与高效调优完全指南
ide·macos·webstorm
独隅2 天前
WebStorm macOS 安装调优与实战效能全景
ide·macos·webstorm
2601_967987372 天前
安装 Redis 或 Redis Stack
linux·windows·redis·macos·安装
高调的涵涵3 天前
1GB 文件,df 说没占地方
macos
茶底世界之下3 天前
窗边白雏菊与旧书:用 Awaking 留住一束暖光
ios·app·产品
奶油话梅糖3 天前
苹果的内核跑上了安卓手机:2,631 Star 的 HakiOS 让 XNU 在 Adreno GPU 上画出 118 帧
android·macos·智能手机
独隅4 天前
WebStorm macOS 安装调优与实战效果全景
ide·macos·webstorm
茶底世界之下4 天前
阴天海边的蓝船:用 Awaking 把灰蓝调成清亮主角
ios·app·产品
木子欢儿4 天前
国内 macOS/Linux 安装 rust
linux·运维·服务器·macos·rust