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("")
}
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()
....
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With