# SwiftUI **Repository Path**: TheAlgorithms/SwiftUI ## Basic Information - **Project Name**: SwiftUI - **Description**: SwiftUI 布局框架的一些官方示例,希望对你了解和学习这门新布局框架有所帮助 - **Primary Language**: Swift - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 256 - **Forks**: 67 - **Created**: 2019-06-05 - **Last Updated**: 2025-06-19 ## Categories & Tags **Categories**: cross-platform-mobiledev **Tags**: None ## README [![Build Status](https://img.shields.io/badge/platforms-iOS%20%7C%20tvOS%20%7C%20macOS%20%7C%20watchOS-green.svg)](https://github.com/Jinxiansen/SwiftUI) [![Swift](https://img.shields.io/badge/Swift-5.1-orange.svg)](https://swift.org) [![Xcode](https://img.shields.io/badge/Xcode-11.0-blue.svg)](https://developer.apple.com/xcode) [![Xcode](https://img.shields.io/badge/macOS-15.0-blue.svg)](https://developer.apple.com/macOS) [![MIT](https://img.shields.io/badge/licenses-MIT-red.svg)](https://opensource.org/licenses/MIT) 本文参考 SwiftUI [官方示例](https://github.com/Jinxiansen/SwiftUI/tree/doc) 并将探索结果记录于此,希望能够对你有所帮助。 对于本文所述内容,默认你已有一定的基于 Swift 语言的开发经验,故不会详细的叙述每个细节;如果对 Swift 语法有疑问,可先学习 [Swift](https://swift.org) 语法。 有关 `SwiftUI` 的疑问,可加入 SwiftUI QQ 交流群:**18552966** ,共同探讨。 [English 📔](README.md) ### [Whats New in SwiftUI?](https://developer.apple.com/xcode/swiftui/) ## 截图 |View|Layout| |:---:|:---:| ||| ## 💻 所需环境 - macOS 10.15 - Xcode 11.0 - iOS 13.0 ## 📂 目录: ### 基础控件 * Text 文本 - [Text](#Text) - [TextField](#TextField) - [SecureField](#SecureField) * Image 图片 - [Image](#Image) - [WebImage](#WebImage) * Button 按钮 - [Button](#Button) - [PullDownButton](#PullDownButton) - [ItemBasedPopUpButton](#ItemBasedPopUpButton) - [NavigationButton](#NavigationButton) - [PresentationButton](#PresentationButton) - [EditButton](#EditButton) - [PasteButton](#PasteButton) * Picker 选择器 - [Picker](#Picker) - [DatePicker](#DatePicker) - [Toggle](#Toggle) - [Slider](#Slider) - [Stepper](#Stepper) - [SegmentedControl](#SegmentedControl) * 特殊视图 - [WebView](#WebView) - [UIViewController](#UIViewController) ### 布局 * Stacks - [HStack](#HStack) - [VStack](#VStack) - [ZStack](#ZStack) * List 列表 - [List](#List) - [ScrollView](#ScrollView) - [ForEach](#ForEach) * Container Views 容器视图 - [Group](#Group) - [GroupBox](#GroupBox) - [Section](#Section) - [Form](#Form) * Architectural Views 导航、切换、排列 - [NavigationView](#NavigationView) - [TabView](#TabView) - [HSplitView](#HSplitView) - [VSplitView](#VSplitView) * Alert 弹框、选择 - [Alert](#Alert) - [Modal](#Modal) - [Popover](#Popover) - [Sheet](#Sheet) - [ActionSheet](#ActionSheet) ### State and Data Flow 状态和数据流 * Bindings * [Binding](#Binding) * Data-Dependent Views * [State](#State) * [ObjectBinding](#ObjectBinding) * [EnvironmentObject](#EnvironmentObject) * Environment Values * [Environment](#Environment) * [EnvironmentValues](#EnvironmentValues) * ENavigation Models * [DynamicNavigationDestinationLink](#DynamicNavigationDestinationLink) * Preferences * [LocalizedStringKey](#LocalizedStringKey) * Transactions * [Transaction](#Transaction) ### 手势 * Basic Gestures 基本手势 * [TapGesture](#TapGesture) * [LongPressGesture](#LongPressGesture) * [DragGesture](#DragGesture) * [MagnificationGesture](#MagnificationGesture) * [RotationGesture](#RotationGesture) * Combined Gestures 合并手势 * [SequenceGesture](#SequenceGesture) * [SimultaneousGesture](#SimultaneousGesture) * [ExclusiveGesture](#ExclusiveGesture) * Custom Gestures 自定义手势 * [AnyGesture](#AnyGesture)

基础控件

Text

`Text` 用来展示一行或多行的文本内容,效果等同于 `UILabel`,但更加优秀。 如果要创建 `Text`, 只需通过 `Text("SwiftUI")` 即可创建; 采用链式语法,也可以为文本添加多项属性,如字体、颜色、阴影、上左下右的间距等。 示例: ```swift Text("SwiftUI") .foregroundColor(.orange) .bold() .font(.system(.largeTitle)) .fontWeight(.medium) .italic() .shadow(color: .black, radius: 1, x: 0, y: 2) ```
查看运行效果
> HStack 和 VStack 控件用于承载多个视图,在后面会提到。 [🔝](#Text_D)

TextField

`TextField` 用来添加普通的输入框,一般常用作文本输入。 示例: ```swift TextField(self.$name, placeholder: self.nameText, onEditingChanged: { changed in print("onEditing: \(changed)") }) { print("userName: \(self.name)") self.endEditing(true) }} .padding(10) .frame(height: 50) .textFieldStyle(RoundedBorderTextFieldStyle()) .padding(EdgeInsets(top: 0, leading: 20, bottom: 0, trailing: 20)) ```
查看运行效果
[🔝](#Text_D)

SecureField

`SecureField ` 一般作为密码输入时使用,使用方式与 `TextField` 并无差别,示例与运行效果同上 `TextField`。

Image

`Image` 控件用于展示图片。 示例: ```swift Image("icon") .resizable() .frame(width: 100, height: 100, alignment: .center) ```
查看运行效果
[🔝](#Text_D)

WebImage

`webImage` 用于下载网络图片,使用的 `URLSession`下载成功后替换原有 `Image`;你也可以在 `downloadWebImage ` 方法中使用 [Kingfisher](https://github.com/onevcat/Kingfisher) 。 示例: ```swift var body: some View { Image(uiImage: self.uiImage ?? placeholderImage) .resizable() .onAppear(perform: downloadWebImage) .frame(width: 80, height: 80, alignment: .center) .onTapGesture { print("Tap ") } } ```
查看运行效果
[🔝](#Text_D)

Button

`Button` 用于响应点击事件。 示例: ```swift Button(action: { print("Tap") }) { Text("I'm a Button") } ```

PullDownButton

尚未发布

ItemBasedPopUpButton

尚未发布 `NavigationButtonPage ` 用以 Push 到下一个导航页面。 示例: ```swift NavigationLink(destination: NavigationButtonPage()) { Text("NavigationButton").bold() .foregroundColor(.orange) .font(.largeTitle) } .navigationBarTitle(Text("Page")) ```
查看运行效果
[🔝](#Button_D)

PresentationButton

`PresentationButton` ~~用以弹出一个页面。~~ 已经弃用了,请使用 `NavigationLink` [🔝](#Button_D)

EditButton

`EditButton` 用以触发编辑状态,使用时只需在 `navigationBarItems ` 设置即可。 示例: ```swift navigationBarItems(trailing: EditButton()) ```
查看运行效果
[🔝](#Button_D)

PasteButton

尚未发布

Picker

`Picker` 可自定义数据源的选择器。 示例: ```swift Picker(selection: $leftIndex, label: Text("Picker")) { ForEach(0.. 查看运行效果 [🔝](#Picker_D)

DatePicker

`DatePicker` 用于选择绝对日期的控件。 示例: ```swift DatePicker(selection: $server.date, in: server.spaceDate, displayedComponents: .date, label: { Text("") }) ```
查看运行效果
[🔝](#Picker_D)

Toggle

`Toggle` 用于切换选中状态。 示例: ```swift Toggle(isOn: $isOn) { Text("State: \(self.isOn == true ? "开":"关")") }.padding(20) ```
查看运行效果
[🔝](#Picker_D)

Slider

`Slider ` 用于从有限值范围中选值的控件。 示例: ```swift Slider(value: $data.rating) ```
查看运行效果
[🔝](#Picker_D)

Stepper

`Stepper ` 用以增加或减少数值。 示例: ```swift Stepper(value: $value, step: 2, onEditingChanged: { c in print(c) }) { Text("Stepper Value: \(self.value)") }.padding(50) ```
查看运行效果
[🔝](#Picker_D)

SegmentedControl 已经弃用了

~~`SegmentedControl` 用以分段条件选择。~~ 示例: ```swift SegmentedControl(selection: $currentIndex) { ForEach(0.. 查看完整示例及运行效果 [🔝](#Picker_D)

WebView

`WebView` 用于展示一个打开的网页。 示例: ```swift struct WebViewPage : UIViewRepresentable { func makeUIView(context: Context) -> WKWebView { return WKWebView() } func updateUIView(_ uiView: WKWebView, context: Context) { let req = URLRequest(url: URL(string: "https://www.apple.com")!) uiView.load(req) } } ```
查看运行效果
[🔝](#Special_D)

UIViewController

`UIViewController ` 用于展示在 **SwiftUI** 中打开 **UIKit** 的 **UIViewController** ,并且在 **UIViewController** 中打开 `SwiftUI` View。 示例: 先定义: ```swift struct ControllerPage : UIViewControllerRepresentable { typealias UIViewControllerType = UIViewController func makeUIViewController(context: UIViewControllerRepresentableContext) -> UIViewController { return T() } func updateUIViewController(_ uiViewController: UIViewController, context: UIViewControllerRepresentableContext) { debugPrint("\(#function):\(type(of: T.self))") } } ``` 然后调用: ```swift NavigationButton(destination: ControllerPage()) { PageRow(title: "UIViewController",subTitle: "打开 UIViewController") } ```
查看运行效果
[🔝](#Special_D) ### 布局

HStack

`HStack` 用于将子视图排列在水平线上的视图。 示例: ```swift HStack { Text("made in China.") Divider() // Just add a line. Text("the People's Republic Of China.") } ```
查看运行效果
[🔝](#Layout_D)

VStack

`VStack` 用于将子视图排列在垂直线上的视图。 示例: ```swift VStack { Text("made in China.") Divider() // Just add a line. Text("the People's Republic Of China.") } ```
查看运行效果
[🔝](#Layout_D)

ZStack

`ZStack` 用于覆盖子视图,在两轴上对齐。 示例: ```swift ZStack { Text("made in China.") Divider() // Just add a line. Text("the People's Republic Of China.") } ```
查看运行效果
[🔝](#Layout_D)

List

`List` 列表容器,用以显示一列数据。 示例: ```swift List(0..<5) { item in Text("Hello World !") }.navigationBarTitle(Text("List"), displayMode: .large) ```
查看运行效果
[🔝](#Layout_D)

ScrollView

`ScrollView` 是一个滚动视图容器。 示例: ```swift ScrollView { Text("SwiftUI").padding(20) Divider() Image("icon").resizable() .frame(width: 300, height: 300, alignment: .center) Divider() Text("Views and ... user interface.") } .border(Color.gray.gradient, width: 1) .cornerRadius(10) .padding(10) .navigationBarTitle(Text("ScrollView")) ```
查看运行效果
[🔝](#Layout_D)

ForEach

`ForEach` 用于根据已有数据的集合展示视图。 示例: ```swift let data = (0..<5) var body: some View { ForEach(data) { e in Text("Hello \(e)") .bold() .font(.system(size: 25, design: .monospaced)) .padding(5) } ```
查看运行效果
[🔝](#Layout_D)

Group

`Group` 用于集合多个视图,对 Group 设置的属性,将作用于每个子视图。 示例: ```swift Group { Text("Hello World !") Text("Hello World !") } ```
查看运行效果
[🔝](#Layout_D)

GroupBox

尚未发布

Section

`Section` 用于创建带头/尾部的视图内容,一般结合 `List` 组件使用。 示例: ```swift Section(header: Text("I'm header"), footer: Text("I'm footer")) { ForEach(0..<3) { Text("Hello \($0)") } } ```
查看运行效果

Form

`Form` 是对一组数据输入进行控制的容器。 Example: ```swift Form { TextField("First Name", text: $firstName) TextField("Last Name", text: $lastName) } ```
查看运行效果
[🔝](#Layout_D) `NavigationView` 用于创建包含顶部导航栏的视图容器。 示例: ```swift NavigationView { Text("🧚‍♂️🧚‍♀️🧜‍♂️🧜‍♀️🧞‍♂️🧞‍♀️").blur(radius: 5) Text("Swifter Swifter") .bold() .foregroundColor(.orange) .font(.largeTitle) } .navigationBarTitle(Text("NavigationView")) ```
查看运行效果
[🔝](#Layout_D)

TabView

`TabView` 用于创建包含底部 ** TabBar** 的视图容器。 示例: ```swift TabView(selection: $index) { ForEach(0.. 查看运行效果 [🔝](#Layout_D)

HSplitView

尚未发布

VSplitView

尚未发布

Alert

`Alert` 用于展示一个弹框提醒,需要与点击事件关联起来。 示例: ```swift alert(isPresented: $showAlert, content: { Alert(title: Text("确定要支付这100000000美元吗?"), message: Text("请谨慎操作\n一旦确认,钱款将立即转入对方账户"), primaryButton: .destructive(Text("确认")) { print("转出中...") }, secondaryButton: .cancel()) }).navigationBarTitle(Text("Alert")) ```
查看运行效果
[🔝](#Alert_D)

ActionSheet

`ActionSheet` 用于弹出一个选择框。 示例: ```swift ActionSheet(title: Text("Title"), message: Text("Message"), buttons: [.default(Text("Default"), onTrigger: { print("Default") self.showSheet = false }),.destructive(Text("destructive"), onTrigger: { print("destructive") self.showSheet = false }),.cancel({ print("Cancel") self.showSheet = false })]) ``` 使用: ```swift .actionSheet(isPresented: $showSheet, content: {sheet}) ```
查看运行效果
[🔝](#Alert_D) `Modal` 用于弹出一个视图。 示例: ```swift Modal(Text("Modal View"),onDismiss: { print("View Dismiss !") }) ```
查看运行效果
[🔝](#Alert_D)

Popover

`Popover` 用于弹出一个视图,样式见下方运行结果。 示例: ```swift .popover(isPresented: $showPop, content: { ImagePage() }) ```
查看运行效果
[🔝](#Alert_D) ## 📎 About * 以上示例中所涉及代码,皆在本仓库代码中,建议下载并运行查看。 * 如果有关于 SwiftUI 更好的用法与建议,期待能够一起分享! * 如果本文示例内容有疏漏和错误之处,欢迎提 [**Issue**](https://github.com/Jinxiansen/SwiftUI/issues/new) ! ## ✉️ Contacts email : hi@jinxiansen.com 微博 : [@晋先森](http://weibo.com/3205872327) ## 📄 License SwiftUI is released under the [MIT license](LICENSE). See LICENSE for details.