Lars
Lars

Reputation: 33

SwiftUI Picker problem after dismissing .fullScreenCover or .sheet

I have a picker that works fine until after showing and dismissing a fullScreenCover or a sheet. Does anyone know what the problem is with this sample code, or have a work-around?

I have tried dismissing the sheet using self.presentation.wrappedValue.dismiss() as well, but with the same result.

Example gif: https://i.sstatic.net/zmcmv.gif

Code:

import SwiftUI

struct ContentView: View {
    @State var selectedFilterStatus = ActiveStatus.active
    @State var showDetail = false
    
    var body: some View {
        NavigationView {
            VStack {
                Button(action: {
                    showDetail.toggle()
                }, label: {
                    Text("Detail popup")
                })
                
                Picker("\(selectedFilterStatus.title)", selection: $selectedFilterStatus) {
                    Text(ActiveStatus.active.title).tag(ActiveStatus.active)
                    Text(ActiveStatus.inactive.title).tag(ActiveStatus.inactive)
                }
            }
            .fullScreenCover(isPresented: $showDetail, content: {
                MyDetailsView(presenting: $showDetail)
            })
        }
        .navigationTitle("Main")
    }
}

struct MyDetailsView: View {
    @Binding var presenting: Bool
    
    var body: some View {
        VStack {
            Text("Hello from details!")
            Button(action: {
                presenting.toggle()
            }, 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"
            
        }
    }
}

Upvotes: 3

Views: 1408

Answers (1)

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"
        }
    }
}

Upvotes: 2

Related Questions