SwiftUI Form Picker only shows once
Asked Answered
C

2

10

I am playing around with SwiftUI, and am currently building a Form using a Picker.

import SwiftUI

struct ContentView: View {
  private let names = ["Bill", "Peter", "Johan", "Kevin"]
  @State private var favoritePerson = "Bill"

  var body: some View {
    NavigationView {
      Form {
        Picker("Favorite person", selection: $favoritePerson) {
          ForEach(names, id: \.self) { name in
            Text(name)
          }
        }
      }
      .navigationBarTitle("Form", displayMode: .inline)
    }
  }
}

The first time you tap on the "Favorite person" row, the picker shows up fine, and tapping on one of the names brings you back to the form. But tapping on the form row a second time doesn't do anything: you don't go to the picker, the row stays highlighted but nothing happens. If this is a SwiftUI bug, is there a known workaround? (I already needed to use a small navigation bar title to work around the Picker UI bug where otherwise its content moves up when it's shown ☹️)

Conqueror answered 1/1, 2020 at 14:11 Comment(4)
Works fine with Xcode 11.2. If you use 11.3, just downgrade.Lessard
Isn't that just going to reintroduce a bunch of other bugs though? (Also, come on Apple! Ugh..)Conqueror
As far as I observe 11.3 introduced just huge amount of broken areas, and 11.1 was most stable.Lessard
Don't think this is limited to Form / Picker. I seem to get this with a simple NavigationLink whereby the initial tap works all fine but subsequent taps do nothing e.g. struct ContentView: View { var body: some View { NavigationView { Section { NavigationLink(destination: Text("Hello")) { Text("Tap me...") } } .navigationBarTitle("Demo") } } } I'm using Xcode 11.3Jordanson
C
7

this issue is just one with the simulator. If you build the app on a physical iOS device, it no longer becomes an issue. It's like that bug with Navigation Link that would only work once.

Center answered 8/1, 2020 at 21:7 Comment(1)
You are correct, it's a problem with NavigationLink, the form picker was just a red herring really.Conqueror
R
1

I have the same problem in Xcode 11.4 and also in the real device. Picker change didn't call CreateTab, it only worked in initialize.

Picker("Numbers", selection: $selectorIndex) {
            ForEach(0 ..< formData.tabs.count) { index in
                Text(formData.tabs[index].name).tag(index)
            }
          }
          .pickerStyle(SegmentedPickerStyle())
          .onReceive([self.selectorIndex].publisher.first()) { (value) in
            print(value)
            CreateTab(tabs: formData.tabs, index: self.selectorIndex)
}
Rutledge answered 4/5, 2020 at 17:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.