Picker without NavigationView

I have a picker on my view but it's not selectable unless I replace my VStack for a NavigationView. However I cant have a NavigationView on this page, otherwise it brakes other parts.

What's the solution for that ? Thx

VStack {
  Picker
}
Answered by Aganeto in 697671022

Found a solution. Replace the form with ScrollView instead of NavigationView and the picker will work :)

I don't understand your point (may be because you did not show code).

  • Here is a working example of Picker:
struct ContentView: View {
    
    var colors = ["Red", "Green", "Blue", "Tartan"]
    @State private var selectedColor = "Red"
    var body: some View {
        //            VStack {  // I remove VStack voluntarily
        HStack {
            Text("Please choose a color")
            Picker("", selection: $selectedColor) {
                ForEach(colors, id: \.self) {
                    Text($0)
                }
            }
            //                }
            Text("You selected: \(selectedColor)")
        }
    }
}

  • If I use a VStack, it works as well:
struct ContentView: View {
    
    var colors = ["Red", "Green", "Blue", "Tartan"]
    @State private var selectedColor = "Red"
    var body: some View {
        VStack {
            HStack {
                Text("Please choose a color")
                Picker("", selection: $selectedColor) {
                    ForEach(colors, id: \.self) {
                        Text($0)
                    }
                }
            }
            Text("You selected: \(selectedColor)")
        }
    }
}

Note : text does not show in Picker

read: h t t p s : / / w w w.hackingwithswift.com/forums/swiftui/swiftui-xcode-12-2-picker-no-longer-shows-label/4809

Here what I've figured out:

  1. If a picker is inside a Form, it's going to be frozen and not selectable
Form {				
   Picker("Choose a number", selection: $selection) {					
            ForEach(0 ..< data.count, id: \.self) { index in						                  
                 Text(data[index])					
            }				
    }			
}
  1. If I wrap this form in a NavigationView, it's going to work perfectly.
NavigationView {			
    Form {				
        Picker("Choose a number", selection: $selection) {					
                 ForEach(0 ..< data.count, id: \.self) { index in						
                      Text(data[index])					
                 }				
         }			
}

However for my current design, I can't have a NavigationView on this View. Is there a solution for that ? Thx

If a picker is inside a Form, it's going to be frozen and not selectable

Do you need a Form or not ? You didn't say in your OP.

If you search you will find it is a known bug in SwiftUI.

https://stackoverflow.com/questions/66275021/picker-appears-disabled-inside-a-form-bug/66275172

So, all you can do:

  • add a NavigationView
  • or file a bug report and wait
Accepted Answer

Found a solution. Replace the form with ScrollView instead of NavigationView and the picker will work :)

Picker without NavigationView
 
 
Q