Post

Replies

Boosts

Views

Activity

SwiftUI picker jumps during state var update
Hello everyone!I'm having an issue where I would like to use a picker, while having state updates coming in from an external source, e.g. a timer. When I do this with ForEach() inside the Picker, I find that the picker 'jumps' when scrolling and an unrelated state var changes. If I list several static choices in the picker, it works correctly.Is this an issue with Picker? Any ideas on how to work around this?Here's some sample code that shows this issue (in Xcode 11.3):import SwiftUI struct ContentView: View { let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect() @State private var counter = 0 @State private var pickerSelection = 0 var body: some View { VStack { Text("Counter: \(counter)") .onReceive(timer) { input in self.counter += 1 print(input) } Picker("Snappy Picker", selection: self.$pickerSelection) { // This does not work - picker snaps to original value when counter changes: ForEach(0..<8) { i in Text("Item \(i)") } // This works - picker doesn't snap when counter changes: /* Text("Item 0") Text("Item 1") Text("Item 2") Text("Item 3") Text("Item 4") Text("Item 5") Text("Item 6") Text("Item 7") Text("Item 8") */ } .labelsHidden() } } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }
3
2
6.9k
Dec ’19