The Picker is a SwiftUI view that presents a set of options for the user to choose from. It can be displayed as a dropdown menu, a segmented control, or a wheel, depending on the context and the number of options available.

Use Case: Picker

A common use case for Picker is to allow users to select a value from a predefined list. For example, you might use a Picker to let users choose their favorite fruit from a list of options.

Set up Picker cases

We need to define the options for our picker. In this case, we'll create an enum for the different themes.

We conform it to String and add conformance to CaseIterable (to support .allCases) and Identifiable (so that we can use it in a ForEach loop)

enum Theme: String, CaseIterable, Identifiable {
    case light = "Light"
    case dark = "Dark"
    case system = "System Default"

    var id: String { self.rawValue }
}

Picker View

struct PickerView: View {
    @State private var selectedTheme: Theme = .light
    var body: some View {
        Form {
            Section("Default/Automatic") {
                Picker("Select Theme", selection: $selectedTheme) {
                    ForEach(Theme.allCases) { theme in
                        Text(theme.rawValue)
                            .tag(theme)
                    }
                }
                .pickerStyle(.automatic)
            }
        }
    }
}

Different Picker styles

The Picker can be styled in various ways to suit different use cases. Here are some of the available styles:

Style Description
.automatic The system chooses the best style based on the context.
.menu A menu-style picker that presents options in a list.
.segmented A segmented control style picker for a compact selection.
.navigationLink A picker that navigates to a new view for selection.
.palette A color palette style picker for selecting colors.
.inline An inline style picker that displays options within the form.
.wheel A wheel-style picker for selecting values in a scrollable wheel.

In this code example, we create a PickerView that demonstrates the different picker styles:

struct PickerView: View {
    @State private var selectedTheme: Theme = .light
    var body: some View {
        NavigationStack {
            Form {
                Section("Default/Automatic") {
                    Picker("Select Theme", selection: $selectedTheme) {
                        ForEach(Theme.allCases) { theme in
                            Text(theme.rawValue)
                                .tag(theme)
                        }
                    }
                    .pickerStyle(.automatic)
                }

                Section(".menu") {
                    Picker("Select Theme", selection: $selectedTheme) {
                        ForEach(Theme.allCases) { theme in
                            Text(theme.rawValue).tag(theme)
                        }
                    }
                    .pickerStyle(.menu)
                }

                Section(".segmented") {
                    Picker("Select Theme", selection: $selectedTheme) {
                        ForEach(Theme.allCases) { theme in
                            Text(theme.rawValue).tag(theme)
                        }
                    }
                    .pickerStyle(.segmented)
                }

                Section(".navigationLink") {
                    Picker("Select Theme", selection: $selectedTheme) {
                        ForEach(Theme.allCases) { theme in
                            Text(theme.rawValue).tag(theme)
                        }
                    }
                    .pickerStyle(.navigationLink)
                }

                Section(".palette") {
                    Picker("Select Theme", selection: $selectedTheme) {
                        ForEach(Theme.allCases) { theme in
                            Text(theme.rawValue).tag(theme)
                        }
                    }
                    .pickerStyle(.palette)
                }

                Section(".inline") {
                    Picker("Select Theme", selection: $selectedTheme) {
                        ForEach(Theme.allCases) { theme in
                            Text(theme.rawValue).tag(theme)
                        }
                    }
                    .pickerStyle(.inline)
                }

                Section(".inline & labelsHidden") {
                    Picker("Select Theme", selection: $selectedTheme) {
                        ForEach(Theme.allCases) { theme in
                            Text(theme.rawValue).tag(theme)
                        }
                    }
                    .pickerStyle(.inline)
                    // I'd suggest to hide the "labels" it looks cleaner when using .inline
                    .labelsHidden()
                }

                Section(".wheel") {
                    Picker("Select Theme", selection: $selectedTheme) {
                        ForEach(Theme.allCases) { theme in
                            Text(theme.rawValue).tag(theme)
                        }
                    }
                    .pickerStyle(.wheel)
                }
            }
        }
    }
}

This is a screenshot of the different picker styles in action.
Picker

Conclusion

Pickers are a powerful way to allow users to select from a range of options in a SwiftUI application. By utilizing the various picker styles available, you can create a more intuitive and user-friendly interface.

Resources:

Read more

Share


Share Bluesky Mastodon Twitter LinkedIn Facebook