0

I'm seeing very strange behavior for code that previously worked as expected in Monterey. The first click opens an invisible sheet (without showing the contents for the selection). If you use the escape key to close the sheet you can click on any other item in the list and it will open and show the contents. But if you click the same item the second time it does not.

import SwiftUI

struct Person: Identifiable {
    let id = UUID()
    var name: String
    var phoneNumber: String
}

struct ContactCard: View {
    var person: Person
    
    var body: some View {
        VStack {
            Image(systemName: "person")
                .font(.title)
            Text("\(person.name)")
                .font(.headline)
        }
        .padding()
    }
}

struct ContentView: View {
    var staff = [
        Person(name: "Juan Chavez", phoneNumber: "(408) 555-4301"),
        Person(name: "Mei Chen", phoneNumber: "(919) 555-2481")
    ]
    @State var showModal = false
    @State var selectedPerson: Person? = nil
    
    var body: some View {
        List {
            ForEach(staff) { person in
                Text(person.name)
                    .onTapGesture {
                        selectedPerson = person
                        showModal = true
                    }
            }
            .sheet(isPresented: $showModal) {
                if let person = selectedPerson {
                    ContactCard(person: person)
                        .onTapGesture {
                            showModal = false
                        }
                }
            }
        }
        .padding()
    }
}

This is an example I created to isolate the issue from my actual project for posting here. What is really strange is that in my original project it still works correctly in iOS but not in macOS. But this new project I made to isolate the behavior has the same issue in iOS and macOS. On iOS I'm seeing the following message in the Console:

2022-11-21 10:29:15.735014-0800 FirstClickTest2[793:171365] [Presentation] Attempt to present <TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView: 0x10680c800> on <TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier_: 0x105820400> (from <TtGC7SwiftUI19UIHostingControllerGVS_15ModifiedContentVS_7AnyViewVS_12RootModifier_: 0x105820400>) which is already presenting <TtGC7SwiftUI29PresentationHostingControllerVS_7AnyView: 0x106810a00>.

I am not getting this message in my original project where the first click opens the sheet with its contents correctly. And the message does not show on macOS where it is loading the empty sheet on the first click.

I thought the issue might be related to this one here but that one uses navigation instead of sheets, so even if using onAppear to select the first item when the page loads did solve the problem it wouldn't work for me (I tried it just to see, and it did not work).

I have also tried delaying the sheet with a timeout after setting the selection like this:

.onTapGesture {
    selectedPerson = person
    DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
        showModal = true
    }
 }

The behavior is the same (except for the 1 second delay).

It's also strange that after the sheet does appear on macOS (after the second click), when dismissing the sheet it animates away, then animates back in, and then animates away again. My original project doesn't exhibit this behavior either, and there's no difference in the code related to showing or dismissing the sheet that I can see. The only difference I can think of is that the example project was created with Xcode 14, while my original project was made in Xcode 11 or 12.

If anyone can help me understand what going on or point me to another way to implement this that doesn't have this issue, it would be much appreciated.

Subcreation
  • 1,353
  • 12
  • 26
  • 1
    Use sheet(item:) – lorem ipsum Nov 21 '22 at 19:39
  • Does this answer your question? [SwiftUI: Understanding .sheet / .fullScreenCover lifecycle when using constant vs @Binding initializers](https://stackoverflow.com/questions/65281559/swiftui-understanding-sheet-fullscreencover-lifecycle-when-using-constant-v) – lorem ipsum Nov 21 '22 at 19:40
  • @loremipsum Thank you—that was exactly what I needed. If you want to post this as the answer, I'll mark it as the solution – Subcreation Nov 21 '22 at 19:53
  • That is, sheet(item:) worked for me. Thanks for providing the link so I can learn more about what's going on – Subcreation Nov 21 '22 at 19:53

0 Answers0