nets-rac
nets-rac

Reputation: 85

SwiftUI Picker in Forms (iOS 16)

Before iOS 16 picker views take on special behavior when inside forms. They looked like a navigation link which takes you to a new screen where you can choose an option. Since iOS 16 it seems, that this behavior was removed.

Is there a possibility to get the "old" behavior?

e.g. this code

struct ContentView: View {
    @State private var selectedValue = "One"
    let counts = ["One", "Two", "Three"]

    var body: some View {
        NavigationView {
            Form {
                Section {
                    Picker("Selection", selection: $selectedValue) {
                        ForEach(counts, id: \.self) {
                            Text($0)
                        }
                    }
                }
            }
        }
    }
}

results in this behavior (since iOS 16)

picker iOS 16

instead of this (before iOS 16)

Picker Style in Forms before iOS 16

Picker Style in Forms before iOS 16

Thanks!!!

Upvotes: 6

Views: 5675

Answers (2)

comexbackkid
comexbackkid

Reputation: 31

I'm in a similar boat, I'm currently downloading the 14.1 Beta of Xcode, however it WOULD be nice if you could have the menu style picker but also have it display nothing. It's almost as if you already have something selected, but you don't. It causes confusion with the user.

Upvotes: 0

Carsten
Carsten

Reputation: 1081

iOS 16 added NavigationLinkPickerStyle which has the pre iOS 16 behavior.

struct ContentView: View {
    @State private var selectedValue = "One"
    let counts = ["One", "Two", "Three"]

    var body: some View {
        NavigationView {
            Form {
                Section {
                    if #available(iOS 16.0, *) {
                        Picker("Selection", selection: $selectedValue) {
                            ForEach(counts, id: \.self) {
                                Text($0)
                            }
                        }
                        .pickerStyle(.navigationLink)
                    } else {
                        Picker("Selection", selection: $selectedValue) {
                            ForEach(counts, id: \.self) {
                                Text($0)
                            }
                        }
                    }
                }
            }
        }
    }
}

Upvotes: 8

Related Questions