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
    }
}
相关推荐
2501_9159090618 小时前
手机崩溃日志导出的工程化体系,从系统级诊断到应用行为分析的多工具协同方法
android·ios·智能手机·小程序·uni-app·iphone·webview
无意feel19 小时前
MacOS 安装neofetch cmatrix lolcat
macos·数字雨·彩虹特效
KevinShi_BJ21 小时前
MacOS 上安装 Docker 和启动 LangBot
macos
2501_915106321 天前
App HTTPS 抓包实战解析,从代理调试到真实网络流量观察的完整抓包思路
网络协议·http·ios·小程序·https·uni-app·iphone
ricky_fan1 天前
(25年12月)claude code报错:might not be available in your country
macos·bash
要站在顶端1 天前
iOS自动化测试全流程教程(基于WebDriverAgent+go-ios)
开发语言·ios·golang
2501_916008891 天前
深入理解 iPhone 文件管理,从沙盒结构到开发调试的多工具协同实践
android·ios·小程序·https·uni-app·iphone·webview
james bid1 天前
MacBook Pro 2015 上 XUbuntu 24.04 启用 eGPU (GeForce GTX 1080 Ti) 和核显黑屏问题解决
linux·ubuntu·macos·cuda·egpu
私人珍藏库1 天前
[Mac] Mac风扇控制软件 iFan V1.1.1
macos
腾讯云qcloud07551 天前
腾讯位置商业授权iOS 轨迹SDK
macos·ios·cocoa