---
title: 创建基于 UIKit 的原生 iOS 项目
description: 现在新建项目默认已经是 SwiftUI，但我还是习惯用 UIKit，所以把步骤记在这里。
date: 2026-06-03
navigation:
  icon: i-lucide-app-window
seo:
  title: 用 Xcode 创建纯代码 UIKit 项目
  description: Xcode 16 默认 SwiftUI。把 Interface 设为 Storyboard，删除 Main.storyboard，在 SceneDelegate 里设置根控制器，得到纯代码 UIKit 工程。
links:
  - label: UIKit 文档
    icon: i-simple-icons-apple
    to: https://developer.apple.com/documentation/uikit
    target: _blank
  - label: CocoaPods Guides
    icon: i-lucide-package
    to: https://guides.cocoapods.org
    target: _blank
  - label: Swift Package
    icon: i-simple-icons-swift
    to: https://developer.apple.com/documentation/xcode/adding-package-dependencies-to-your-app
    target: _blank
categories:
  - ios
tags:
  - iOS
  - UIKit
  - Swift
  - Xcode
---

现在用 Xcode 新建 App，默认已经是 SwiftUI。我还是更习惯手写 UIKit，所以把从建工程到跑起来的步骤记一下。

本文用的是 **Xcode 16.5**。目标很简单：不要 Storyboard 拖控件，用纯代码把界面拉起来。

## 前置准备

- 一台 macOS 电脑
- 已经装好 Xcode，文章用的版本是 16.5

## 创建项目

1. 打开 Xcode，选择 **Create New Project**
2. 模板界面先选 **iOS**，再选 **App**，然后点 **Next**
3. 项目配置可以按下面填：
   - **Product Name**：项目名称，例如 `TestApp`
   - **Team**：没有开发者账号就选 **None**
   - **Organization Identifier**：唯一标识符，比如 `com.example`
   - **Interface**：选 **Storyboard**。别直接选 SwiftUI，否则后面没有 `SceneDelegate`，改成 UIKit 会更麻烦
   - **Language**：99% 的情况都选 **Swift**
   - **Testing System**：暂时不需要
   - **Storage**：可选可不选，先不选就行

点 **Next**，选个保存路径，创建项目。

图解步骤：

![创建新项目](https://filebed.chenyiren.top/files/2026/09/2365b38349e606b9aa4a.png){.max-w-md width=753 height=767}

![创建新项目](https://filebed.chenyiren.top/files/2026/09/1f95fdae5e8b5a3ad9ee.jpg){.max-w-md width=751 height=537}

![创建新项目](https://filebed.chenyiren.top/files/2026/09/2018323cc549b5ea9db6.jpg){.max-w-md width=755 height=538}

![创建新项目](https://filebed.chenyiren.top/files/2026/09/a16bbbf27a7374171202.jpg){.max-w-md width=703 height=677}

## 删除 Storyboard

新建出来的项目默认还是走 Storyboard。要纯代码开发，相关文件和配置都得清掉。

1. 删除项目里的 `Main.storyboard`（没有这个文件就跳过）
2. 点项目根目录，切到 **TARGETS** 下的 **Info**
3. 找到 **Main storyboard file base name**，删掉这一条（对应 `UIMainStoryboardFile`）
4. 再展开 **Application Scene Manifest** → **Scene Configuration**，把里面的 **Storyboard Name** 也删掉（对应 `UISceneStoryboardFile`）

两处都要删。只删文件、或者只清其中一处，启动时还会去加载已经不存在的 `Main`，直接崩溃。

**Launch Screen** 那一项不要动，那是启动图，不是主界面。怎么换成自己的图，写在 [设置启动图](/ios/set-launch-screen)。

Build Settings 里如果还能搜到 `UIMainStoryboardFile`，一并清空就行。

![删除 Storyboard](https://filebed.chenyiren.top/files/2026/09/2852957387bb294fee81.jpg){.max-w-md width=1064 height=658}

![删除 Storyboard](https://filebed.chenyiren.top/files/2026/09/022b2485c41f805a6229.jpg){.max-w-md width=1064 height=322}

## 设置根控制器

打开 `SceneDelegate.swift`，用代码手动把窗口和根控制器挂上。Xcode 16 的 App 模板走的是 Scene 生命周期，窗口要挂在 `UIWindowScene` 上，不用再写回以前那种只给 `AppDelegate.window` 赋值的老办法。

```swift [SceneDelegate.swift]
import UIKit

class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    // 核心代码
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        guard let windowScene = (scene as? UIWindowScene) else { return }
        window = UIWindow(windowScene: windowScene)
        window?.rootViewController = ViewController()
        window?.makeKeyAndVisible()
    }
}
```

## 自定义控制器

打开 `ViewController.swift`，先铺一层背景，再加一行居中文本，用来确认窗口已经接到你的控制器。

```swift [ViewController.swift]
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 设置窗口背景颜色
        view.backgroundColor = .systemPink
        // 添加文本
        let label = UILabel()
        label.text = "Hello world!"
        label.textColor = .white
        label.textAlignment = .center
        label.frame = view.bounds
        // 添加到视图
        view.addSubview(label)
    }
}
```

后面如果要加约束，把 `label.frame = view.bounds` 换成 Auto Layout 或 SnapKit 就行。这一步只是验证启动链路通了。

## 运行项目

点 Xcode 左上角的运行按钮，或者按 `Cmd + R`，模拟器就会起来。正常的话，你会看到粉色背景和白色的 `Hello world!`。

![运行项目](https://filebed.chenyiren.top/files/2026/09/5da85eb726a73981719c.jpg){.max-w-md width=1049 height=565}

![运行项目](https://filebed.chenyiren.top/files/2026/09/407419ba6907a13a3964.jpg){.max-w-xs width=446 height=951}

至此，你已经成功创建了一个基于 UIKit 的 Swift 项目：没有 `Main.storyboard`，窗口和根控制器都在 `SceneDelegate` 里创建。

## 安装 CocoaPods

如果你想用 CocoaPods 当包管理器，项目里其实就 4 步。注意：这里不教怎么装 `pod` 环境。

1. 打开终端，切到 iOS 项目根目录（也就是包含 `.xcodeproj` 的那个文件夹）：

```bash
cd /你的项目路径/
```

2. 生成 `Podfile`：

```bash
pod init
```

3. 编辑 `Podfile`，按需引入第三方库：

```ruby [Podfile]
target 'YourProjectName' do
  # 禁用自动包含框架，一般默认即可
  use_frameworks!
  # 引入你需要的第三方库，版本号按需改
  pod 'SnapKit', '~> 5.7'
end
```

4. 保存后，在终端安装依赖：

```bash
pod install
```

安装完成后，根目录会多出一个 `.xcworkspace`。切记：以后请双击打开 `你的项目名称.xcworkspace` 来开发，不要再打开原来的 `.xcodeproj`。

这一步并不难，但在 Xcode 16 里跑 `pod` 脚本，经常会碰到沙盒拦截 `rsync`：

```text
Sandbox: rsync deny(1) file-write-create
.../Build/Products/Debug-iphoneos/YourApp.app/Frameworks/Some.framework
```

解决办法：打开 **Build Settings**，搜索 `sandbox`，把 **Build Options** 里的 **User Script Sandboxing** 改成 **NO**。

![安装 CocoaPods](https://filebed.chenyiren.top/files/2026/09/f62a0694ff46d99e1a82.jpg){.max-w-md width=950 height=851}

## 安装 Swift Package

不想折腾 CocoaPods，用 Xcode 自带的 Swift Package 就行。还是以 SnapKit 为例。同一个库不要两边各加一次。

1. 打开 `.xcodeproj`（不用 workspace）
2. 菜单栏选 **File** → **Add Package Dependencies...**
3. 右上角贴仓库地址，例如：

```text
https://github.com/SnapKit/SnapKit.git
```

4. 依赖规则一般选 **Up to Next Major Version**，点 **Add Package**
5. 确认加到当前 App target，再点 **Add Package**

加完之后，代码里 `import SnapKit` 就能用。根目录会多一个 `Package.resolved`，用来锁定版本，建议一并提交。

也可以点项目根目录，切到 **PACKAGE DEPENDENCIES**，点左下角 **+**，效果一样。

## 下一步

- [设置启动图](/ios/set-launch-screen)：居中 Logo 或铺满屏幕
- 回到 [iOS 分类](/ios) 看同栏目其他笔记
- [路由管理](/ios/routing)：根控制器换成导航控制器之后，用 Router 管 push、弹层和深层链接
- 用 Auto Layout 或 SnapKit 替换 `frame`，避免旋转和不同屏宽时布局乱掉
