SwiftUI Form 中的选取器在选择值后变为灰色

5 swift swiftui ios14

由于某种原因,在我选择一个值后,我的 SwiftUI 表单中的选择器会变成灰色。

我的代码:

Form {
  Section {
    TextField("title", text: $title)
      Picker(selection: $category, label: Text("category")) {
        ForEach(0..<categories.count) { index in
            Text(categories[index]).tag(index)
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这是它在 iPhone 12 模拟器上的样子

Ste*_*aft 5

您需要将表单包装在 NavigationView 中,原因是当您点击选择器时,它将显示另一个页面,因此它需要导航视图才能向后导航。

NavigationView {
  Form {
    Section {
      TextField("title", text: $title)
        Picker(selection: $category, label: Text("category")) {
          ForEach(0..<categories.count) { index in
              Text(categories[index]).tag(index)
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)