1

所以我是 SwiftUI 的新手

我试图在我的视图中有一个表单,我有一个选择器遍历枚举。但是当我点击选择器开始它变灰并且什么都不做时,我必须离开视图然后回来。

我附上了一张关于发生了什么的 GIF。

我已经将我的选择器包装在导航视图中,它第二次起作用了,所以我不认为就是这样。我不确定还有什么可以尝试的。

这是我的快速视图

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()
        }
    }
}
 
4

1 回答 1

0

如果您将 NavigationView 移动到 ContentView,它至少对我来说有效(macos 11.4、xcode 12.5、目标 ios 14.5 和 macCatalyst),如下所示:

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)
    }
}
于 2021-05-21T03:28:57.790 回答