SwiftUI的Picker
本章来记录一下SwiftUI中三种不同Picker的用法 ,分别为normalPicker ,
wheelPicker, segmentedPicker 。可以根据不同需求展示不同的Picker
import SwiftUI
struct PickerBootCamp: View {
@State var selection: String = "Recent"
let titleList = [
"Recent",
"Popular",
"Liked"
]
var body: some View {
NavigationView{
VStack {
Text("slecetion: " + selection)
normalPicker
wheelPicker
segmentedPicker
Spacer()
}.padding()
.navigationTitle("PickerBootCamp")
}
}
var normalPicker: some View {
/// 默认样式
Picker(selection: $selection) {
ForEach(1..<13) { index in
Text("\(index)")
.tag("\(index)")
}
} label: {
}
.pickerStyle(MenuPickerStyle())
}
var wheelPicker: some View {
Picker("Picker", selection: $selection) {
ForEach(1..<13) { index in
Text("\(index)")
.tag("\(index)")
.foregroundColor(.red)
}
}
.pickerStyle(WheelPickerStyle())
}
var segmentedPicker: some View {
Picker("segmented", selection: $selection) {
ForEach(0 ..< titleList.count) { index in
let value = titleList[index]
Text(value)
.tag(value)
}
}
.pickerStyle(SegmentedPickerStyle())
}
}
#Preview {
PickerBootCamp()
}
效果图: