Sheet Display Error When Picker Open

When a Picker is showing, if you tap on a button outside the Picker view that shows a Sheet, the sheet fails to show and subsequently showing any sheet will fail until you restart the app.

This only happens when the button that shows the sheet is too close to the bottom of the screen. If you provide some space between the bottom of the button and the screen (i.e. 50 Points), there is no longer an issue.

I reproduced this with Xcode 14.2 on an iPhone Xs (iOS 16.1.1) and the iPhone 14 Sim (iOS 16.2). It does not occur when testing with Playgrounds.

The error:

[Presentation] Attempt to present <TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView: 0x7fae7585de00> on <TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier_: 0x7fae76011800> (from <TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier_: 0x7fae76011800>) which is already presenting <_UIDatePickerContainerViewController: 0x7fae756b0f00>.


struct ContentView: View {
    
    @State private var showSheet = false
    @State var selectedDate: Date = Date()
    
    var body: some View {
        VStack {
            DatePicker(selection: $selectedDate) {
                Text("Select Date")
            }
            Spacer()
            HStack {
                Button {
                    showSheet = true
                } label: {
                    Text("Show Sheet Working")
                        .background(Color.green)
                }
                .offset(.init(width: 10.0, height: -45.0))
                Button {
                    showSheet = true
                } label: {
                    Text("Show Sheet Broken")
                        .background(Color.red)
                }
                .offset(.init(width: 10.0, height: -35.0))
            }
        }
        .sheet(isPresented: $showSheet) {
            Text("Hello World")
        }
    }
}

To reproduce, run the above code and tap the Date picker at the top. Tapping the green button which has more space from the bottom does not pose the issue. Tapping the red button causes the bug.

I can get the same error, whatever button I tap, if I tap just below the bottom limit of the green button. Or not get any error if I type above the bottom limit of the green button.

Problem, as described in error, is that you present a new sheet when the Picker selection sheet is already presented.

It is not totally clear why it occurs when you tap below some limt in the button. Just as if the sequence of events is different. In one case, pocher is dismissed before presenting, not in the other case.

I solved it by dismissing the Picher with a test on showSheet:

        VStack {
            if !showSheet {  // So when showSheet, picker sheet is hidden before the other is presented
                DatePicker(selection: $selectedDate) {
                    Text("Select Date")
                }
            }
            Spacer()

I’m not sure it is a bug but at least a confusing behavior.

I'm having the same issue on iOS 17.2. While the workaround does work, it's not feasible for my usecase. Would be great if Apple could fix this

The only workaround I found was to wrap the thing you want to present after the date picker is dismissed like this

// Adjust the delay as needed (I've used 0.25 secs with success) 
DispatchQueue.main.asyncAfter(deadline: .now() + delayInSeconds) { 
    showingConfirmationDialog = true
}
Sheet Display Error When Picker Open
 
 
Q