SwiftUI Picker in view disables itself on tap

399 Views Asked by At

So I'm new to SwiftUI

I'm trying to have a form in my view where I have a picker going over an enum. But when I tap the picker to start it greys out and does nothing, I have to leave the view and come back.

I've attached a gif of whats going on.

I've already wrapped my picker in a navigation view, and it works the second time so I don't think that's it. I'm not really sure what else to try.

Here's my swift view

import SwiftUI

enum TestEnum: String, Equatable, CaseIterable {
    case MORNING = "Morning"
    case BREAKFAST = "Breakfast"
    case LUNCH = "Lunch"
    case AFTERNOON = "Afternoon"
    case DINNER = "Dinner"
    case EVENING = "Evening"
    case BEDTIME = "Bedtime"
    case OTHER = "Other or As Needed"

    var id: String { rawValue }
    var localizedName: LocalizedStringKey { LocalizedStringKey(rawValue) }
}

struct FormPage: View {
    @State var selection = TestEnum.AFTERNOON
    
    var body: some View {
        VStack{
            Text("Some Text")
            NavigationView {
                Form {
                    Picker("When do you take this", selection: $selection) {
                        ForEach(TestEnum.allCases, id: \.id) { value in
                            Text(value.rawValue)
                                    .tag(value)
                        }
                    }

                }
            }.navigationViewStyle(StackNavigationViewStyle())
                    .navigationBarHidden(true)
                    .navigationBarBackButtonHidden(true)
        }
    }
}

struct FormPage_Previews: PreviewProvider {
    static var previews: some View {
        FormPage()
    }
}

struct ContentView: View {
    @State private var currentTab = 0
    
    
    var body: some View {
        TabView(selection: $currentTab,
                content: {
                    Text("One")
                    .tag(0)
                    .padding()
                    FormPage()
                        .tag(1)
                        .padding()
                })
                .tabViewStyle(PageTabViewStyle())
                .indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .never))
                .animation(.default)
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}


@main
struct testApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}
 
1

There are 1 best solutions below

0
On

it works, at least for me on (macos 11.4, xcode 12.5, target ios 14.5 and macCatalyst), if you move the NavigationView to the ContentView, like this:

struct FormPage: View {
    @State var selection = TestEnum.AFTERNOON
    
    var body: some View {
        VStack{
            Text("Some Text")
            Form {
                Picker("When do you take this", selection: $selection) {
                    ForEach(TestEnum.allCases, id: \.id) { value in
                        Text(value.rawValue)
                            .tag(value)
                    }
                }
            }
        }
    }
}

struct ContentView: View {
    @State private var currentTab = 0
    
    var body: some View {
        NavigationView {
            TabView(selection: $currentTab,
                    content: {
                        Text("One")
                            .tag(0)
                            .padding()
                        FormPage()
                            .tag(1)
                            .padding()
                    })
                .tabViewStyle(PageTabViewStyle())
                .indexViewStyle(PageIndexViewStyle(backgroundDisplayMode: .never))
                .animation(.default)
        }.navigationViewStyle(StackNavigationViewStyle())
        .navigationBarHidden(true)
        .navigationBarBackButtonHidden(true)
    }
}