[SUI] 表单 (Form)

发布: (2026年1月31日 GMT+8 05:42)
1 min read
原文: Dev.to

Source: Dev.to

Form

Form 是一个用于分组控件的容器,主要用于某些功能的配置。
它以 grouped 样式的 List 展示控件,并在四周留有填充。
List 组件类似,Form 也可以使用 Section 将内容分成多个部分。

示例

enum NotifyMeAboutType {
    case directMessages, mentions, anything
}

enum ProfileImageSize {
    case large, medium, small
}

struct ContentView: View {
    @State private var notifyMeAbout: NotifyMeAboutType = .anything
    @State private var playNotificationSounds = false
    @State private var sendReadReceipts = false
    @State private var profileImageSize: ProfileImageSize = .small

    var body: some View {
        NavigationView {
            Form {
                Section(header: Text("Notifications"),
                        footer: Text("¿Qué quieres que te notifique?")) {
                    Picker("Notify Me About", selection: $notifyMeAbout) {
                        Text("Direct Messages").tag(NotifyMeAboutType.directMessages)
                        Text("Mentions").tag(NotifyMeAboutType.mentions)
                        Text("Anything").tag(NotifyMeAboutType.anything)
                    }
                    Toggle("Play notification sounds", isOn: $playNotificationSounds)
                    Toggle("Send read receipts", isOn: $sendReadReceipts)
                }
                Section(header: Text("User Profiles")) {
                    Picker("Profile Image Size", selection: $profileImageSize) {
                        Text("Large").tag(ProfileImageSize.large)
                        Text("Medium").tag(ProfileImageSize.medium)
                        Text("Small").tag(ProfileImageSize.small)
                    }
                    Button("Clear Image Cache") {}
                }
            }
        }
    }
}
Back to Blog

相关文章

阅读更多 »

[SUI] LabeledContent

LabeledContent(https://developer.apple.com/documentation/swiftui/labeledcontent)是一个将标签附加到控件的容器。基本用法示例:swift struct C…

[SUI] 搜索栏

NavigationStack 中的搜索栏 NavigationStack 可以通过 `searchable` 修饰符添加搜索栏。它的签名是:swift searchable t...

[SUI] 多日期选择器

使用 MultiDatePicker。MultiDatePicker 允许在 SwiftUI 中选择多个日期。Swift 初始化器 MultiDatePicker.init_:selection:in: - titleKey: 标题键。