Wheel Pickers in form disappear after some show/hide cycles
Using .id
for Picker
s 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
How to Draw a Non-Rectangle Uitextview
Property Cannot Be Found in Forward Class Object
How to Represent Core Data Optional Scalars (Bool/Int/Double/Float) in Swift
How to Set the Tab Order in iOS
How to Programmatically Dismiss Uialertcontroller Without Any Buttons
How to Create a Bold Uifont from a Regular Uifont
How to Have a Fixed Uitableview Header While Using Sections
Programmatically Creating Uilabel
How to Change the Development Language in Xcode Before Internationalizing My App
How to Distribute Swift Library Without Exposing the Source Code
Uialertcontroller Handle Dismiss Upon Click Outside (Ipad)
Ios6 Simulator Mkmapkit "Couldn't Find Default.Styleproto in Framework"
Connecting to a Bluetooth Device from iOS, No Mfi
iOS 8 Nsinternalinconsistencyexception
Importing Swift Framework into a Objective-C Project