๐ SwiftUI Tutorials โฉ Working with UI Controls โฉ Sec 3. Define the Profile Editor
VStack(alignment: .leading, spacing: 20) {
Text("Seasonal Photo").bold()
/// โญ๏ธ Picker
Picker("Seasonal Photo", selection: $profile.seasonalPhoto) {
ForEach(Profile.Season.allCases) { season in
Text(season.rawValue).tag(season)
}
}
.pickerStyle(.segmented)
}
๐Implicit Alignment
@State private var selection: String = "leading"
let keys = ["leading", "center", "trailing"]
let alignments: [String:HorizontalAlignment] = [
"leading": .leading, "center": .center, "trailing": .trailing
]
/// segmented control
var picker: some View {
Picker("Align", selection: $selection) {
ForEach(0..<3){ Text(keys[$0]).tag(keys[$0]) }
}
.pickerStyle(.segmented).shadow(radius: 2)
.padding()
// โญ๏ธ animate the change when `selection` changes
.onChange(of: selection) { newSelection in
withAnimation(.easeOut(duration: 1)) {
self.alignment = alignments[newSelection]!
}
}
}