Why Is Swiftui Picker in Form Repositioning After Navigation

Wheel Pickers in form disappear after some show/hide cycles

Using .id for Pickers seems helped. Tested with Xcode 11.2 / iOS 13.2.

var body: some View {
HStack() {
Spacer()
Picker(selection: minutes, label: EmptyView()) {
ForEach((0...9), id: \.self) { ix in
Text("\(ix)").tag(ix)
}
}.pickerStyle(WheelPickerStyle()).frame(width: 50).clipped()
.id(UUID().uuidString)
Text("Min.")
Picker(selection: seconds, label: EmptyView()) {
ForEach((0...59), id: \.self) { ix in
Text("\(ix)").tag(ix)
}
}.pickerStyle(WheelPickerStyle()).frame(width: 50).clipped()
.id(UUID().uuidString)
Text("Sec.")
Spacer()
}
}

SwiftUI Picker problem after dismissing .fullScreenCover or .sheet

I totally agree there is a bug in the system. However, you can get around it.
This is the workaround that works for me, tested on ios-15 and macCatalyst (macos12.01) devices:

import SwiftUI

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

struct ContentView: View {
@State var selectedFilterStatus = ActiveStatus.active
@State var showDetail: ActiveStatus? // <-- here

var body: some View {
NavigationView {
VStack {
Button(action: {
showDetail = ActiveStatus.active // <-- here
}, label: { Text("Detail popup") })

Picker("\(selectedFilterStatus.title)", selection: $selectedFilterStatus) {
Text(ActiveStatus.active.title).tag(ActiveStatus.active)
Text(ActiveStatus.inactive.title).tag(ActiveStatus.inactive)
}.pickerStyle(.menu)

}
// -- here --
.fullScreenCover(item: $showDetail) { _ in
MyDetailsView()
}
}
.navigationViewStyle(.stack)
.navigationTitle("Main")
}
}

struct MyDetailsView: View {
@Environment(\.dismiss) var dismiss // <-- here

var body: some View {
VStack {
Text("Hello from details!")
Button(action: {
dismiss() // <-- here
}, label: {
HStack {
Image(systemName: "chevron.left")
Text("Back")
}
})
}
}
}

enum ActiveStatus: String, CaseIterable, Identifiable {
case active
case inactive

var id: String { self.rawValue }
}

extension ActiveStatus {
var title: String {
switch self {
case .active:
return "Active for sale"
case .inactive:
return "Inactive"
}
}
}

SwiftUI - Can't add modifiers to Picker View within a form

In your code

Picker("Number of coffees had in a day", selection: $coffeeAmount) {
Text("1")
Text("2")
Text("3")
}
.pickerStyle(WheelPickerStyle)
}

You should use WheelPickerStyle() instead of WheelPickerStyle



Related Topics



Leave a reply



Submit