Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI Picker not displaying Options

Tags:

swift

swiftui

I can't seem to find any information on this, anywhere. So I'm learning SwiftUI, and I'm trying to add a Picker to a Form, but the picker won't display the available options when tapped. I don't really know what else to tell you guys so could someone take a look and help me fix this? thanks :)

//Outside the body code
    @State var contacts: [String] = [""]
    @State var types: [Type] = [.mobile]
//Inside the body code and inside a Form{}
    Section(header: Text("Contact")){
            ForEach(0..<types.count, id: \.self) { i in
                HStack{
                    Picker(selection: $types[i], label: Text(""), content: {
                        Text("Mobile").tag(Type.mobile)
                        Text("Land Line").tag(Type.landline)
                        Text("Email").tag(Type.email)
                    }).pickerStyle(DefaultPickerStyle())
                    .frame(width: 80.0)
                    TextField("Insert Detail", text: $contacts[i])
                }
            }
            Button("Add Another") {
                types.append(.mobile)
                contacts.append("")
            }
like image 283
Vulps Avatar asked Oct 30 '25 17:10

Vulps


1 Answers

I recently managed to resolve this problem.

Picker needs to be wrapped in a NavigationView before it will work in a form.

I was able to simply add a NavigationView to the top of my view and put the form inside it, and now the picker works perfectly.

var body: some View{
    NavigationView{
        Form{
            ....
            Picker()
            ....    
        }
    }
}
like image 83
Vulps Avatar answered Nov 02 '25 07:11

Vulps