Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable a Picker option in SwiftUI

Tags:

swift

swiftui

I am trying to disable an option of Picker in swiftUI. According documentation below code is suppose to work:


struct ContentView: View {
    let options = ["All", "Men", "Women", ]
    @State private var selectedOption = "All"

    var body: some View {
        Picker("Options", selection: $selectedOption) {
            ForEach(options, id: \.self) { option in
                Text(option)
                    .disabled(option == "All")
            }
        }
    }

}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Unfortunately, It's not working where I have went wrong. Can anyone help!?

like image 830
S. S. Saruar Jahan Avatar asked Jan 27 '26 23:01

S. S. Saruar Jahan


1 Answers

With iOS 17/macOS 14 selectionDisabled(_:) has been introduced which has the intended effect of keeping the item visible in the picker but disallowing selection.

like image 61
JDS Avatar answered Jan 29 '26 13:01

JDS