Home > OS >  Picker is not showing as it's supposed to be on Xcode Version 14.0.1 (14A400)
Picker is not showing as it's supposed to be on Xcode Version 14.0.1 (14A400)

Time:09-28

While running this code on iPhone 14 Simulator, the picker is not visible on this screen.

@State var gender: String = ""

Picker(selection: $gender,
                        label: Text("Select gender")
                    .font(.headline)
                    .foregroundColor(.purple)
                    .frame(height: 55)
                    .frame(maxWidth: .infinity)
                    .background(Color.white)
                    .cornerRadius(10),
           
                        content:  {
                    Text("1").tag(1)
                    Text("2").tag(2)
                })
                .pickerStyle(MenuPickerStyle())

CodePudding user response:

try using:

@State var gender: Int = 1

Your gender and your tags must match in type (Int), otherwise it does not "work"

EDIT-1: here is my complete test code showing the Picker working:

struct ContentView: View {
    @State var gender = 1

    var body: some View {
        Picker(selection: $gender,label: Text("Select gender")
            .font(.headline)
            .foregroundColor(.purple)
            .frame(height: 55)
            .frame(maxWidth: .infinity)
            .background(Color.white)
            .cornerRadius(10),content: {
            Text("1").tag(1)
            Text("2").tag(2)
        })
        .pickerStyle(MenuPickerStyle())
    }
}

EDIT-2: more code, adding the modifiers to the Picker.

struct ContentView: View {
    @State var gender = 1

    var body: some View {
        Picker(selection: $gender,label: Text("Select gender")){
            Text("1").tag(1)
            Text("2").tag(2)
        }
        .font(.headline)
        .frame(height: 55)
        .frame(maxWidth: .infinity)
        .foregroundColor(.purple)
        .background(Color.green)
        .cornerRadius(10)
        .pickerStyle(MenuPickerStyle())
    }
}
  • Related