Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI Picker greyed out in a List

Does anyone have a fix for this? I have a Picker in my List that doesn't respond to any user input and is greyed out, but if I move it out of the List and into a VStack it functions normally.

The majority of answers I've found to this question all say that the Picker needs to be in a Form to work (which I don't understand since it works fine in a VStack), or that its a bug in which it only works on a physical device, which I've also tested on and gotten the same result.

I'll provide a screenshot as well as the code I'm using below

Picker in simulator

import SwiftUI
struct SettingsView: View {
    
@Binding var age : Int

var body: some View {
        VStack(alignment: .leading){
                List{  
                        Picker("Your age", selection: $age) {
                                ForEach(1...100, id: \.self) { number in
                                    Text("\(number)")
                                           }
                                }
                    }
        }
}
like image 544
A-Fairooz Avatar asked Sep 17 '25 02:09

A-Fairooz


1 Answers

You must embed your List inside a NavigationView, so you can actually "navigate" from "Your age" to the list of Ints.

Just add one additional layer, as shown below:

    var body: some View {
        VStack(alignment: .leading){
            NavigationView {   // This is needed to navigate to the ForEach
                List {
                    Picker("Your age", selection: $age) {
                        ForEach(1..<101) { number in
                            Text("\(number)")
                        }
                    }
                }
            }
        }
    }
like image 196
HunterLion Avatar answered Sep 19 '25 17:09

HunterLion