Search code examples
swiftswiftuiios14xcode12

Using a Picker in a Form inside an Sheet does not work


I need to use a picker in a form inside a sheet. But this does not work. If I click on the picker I get not into the selection of the Elements.

struct ContentView: View {
    @State private var showSheet = false
    
    var body: some View {
        Button("Sheet", action: {
            showSheet.toggle()
        })
        .sheet(isPresented: $showSheet, content: {
            SecondView()
        })
    }
}

struct SecondView: View {
    var body: some View {
        Form {
            Picker(selection: .constant(1), label: Text("Picker")) {
                Text("1").tag(1)
                Text("2").tag(2)
            }
        }
    }
}

Any suggestions here?


Solution

  • It requires NavigationView, ie

        .sheet(isPresented: $showSheet, content: {
            NavigationView {
               SecondView()
            }
        })
    

    alternate is to embed Form in NavigationView directly in SecondView.