SwiftUI(三) Section、Form

SwiftUI’s picker views take on special behavior when inside forms, automatically adapting based on the platform you’re using them with. On iOS this behavior is particularly impressive, because the picker can be collapsed down to a single list row that navigates into a new list of possible options – it’s a really natural way of working with many options.
SwiftUI的选择器视图在内部表单时具有特殊行为,可根据您使用它们的平台自动进行调整。在iOS上,这种行为特别令人印象深刻,因为选择器可以折叠到单个列表行,导航到可能选项的新列表 - 这是一种非常自然的方式来处理许多选项。

例如,这会创建一个带有选择器的表单,该选择器使用数组作为其项目:

import SwiftUI

struct ContentView : View {
    
    @State private var coutryIndex = 0    
    var coutries = ["US","China","Mexico","korea"]

    var body: some View {        
        NavigationView {            
            Form {                
                Section {                    
                    Picker(selection: $coutryIndex, label: Text("Coutry")) {
                        ForEach(0 ..< coutries.count) {
                            Text(self.coutries[$0])
                        }
                    }
                }
            }.navigationBarTitle(Text("Coutry"))
        }
    }
}

#if DEBUG
struct ContentView_Previews : PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}
#endif

效果:

QQ20190705-084226.gif

On iOS, that will appear as a single list row that you can tap to bring in a new screen showing all possible options – Mild, Medium, and Mature. Your current selection will have a checkmark next to it, and when you select a new option it will return to the previous screen with that now showing.
If you want to disable this behavior, you can force the picker to adopts its regular style by using the .pickerStyle(.wheel) modifier, like this:
iOS上,它将显示为单个列表行,您可以点击该行以显示所有可能选项的新屏幕 - 温和,中等和成熟。您当前的选择旁边会有一个复选标记,当您选择一个新选项时,它将返回到前一个屏幕,现在显示该选项。
如果要禁用此行为,可以使用.pickerStyle(.wheel)修饰符强制选取器采用其常规样式,如下所示:

Picker(selection: $selectedStrength, label: Text("Strength")) {
    ForEach(0 ..< strengths.count) {
        Text(self.strengths[$0]).tag($0)
    }
}
    .pickerStyle(.wheel)

一些概念:

Form:

使用表单:
由于公司专注于声明性用户界面,因此SwiftUI为我们提供了一种构建表单的绝佳机制也就不足为奇了 - 用户输入控件的集合旨在收集信息,例如订单表单或设置屏幕。
更好的是,SwiftUI的几个部分自动适应放置在表单中 - 它们的外观和行为发生变化,因此它们与其他输入控件组一起工作得更好。
如果你认为这很聪明,你会喜欢这个:SwiftUI实际上动态地调整布局,以便它可以根据你的代码运行的平台自动为我们制作全新的屏幕 - 它完全解耦了目的我们的视觉控制。这意味着我们描述了我们想要的东西,SwiftUI找出了在当前平台上实现这一目标的惯用方法。
提示:表格就像VStack一样是常规容器,因此您可以根据自己的目的自由切换。

Section:

如何将节添加到列表中:
SwiftUI的列表视图内置了对节和节头的支持,就像UIKit中的UITableView一样。要在某些单元格周围添加一个部分,首先在其周围放置一个部分,也可以添加页眉和页脚。

举个例子,这里有一行包含提醒应用的任务数据:

struct TaskRow: View {
    var body: some View {
        Text("Task data goes here")
    }
}

我们要做的是创建一个包含两个部分的列表视图:一个用于重要任务,一个用于不太重要的任务。如下代码所示:

struct ContentView : View {
    var body: some View {
        List {
            Section(header: Text("Important tasks")) {
                TaskRow()
                TaskRow()
                TaskRow()
            }

            Section(header: Text("Other tasks")) {
                TaskRow()
                TaskRow()
                TaskRow()
            }
        }
    }
}
image.png

参考链接:
https://www.hackingwithswift.com/quick-start/swiftui/working-with-forms
https://www.hackingwithswift.com/quick-start/swiftui/pickers-in-forms
https://www.hackingwithswift.com/quick-start/swiftui/how-to-add-sections-to-a-list

你可能感兴趣的:(SwiftUI(三) Section、Form)