ios – .sheet dismissed instantly when offered over DisclosureGroup

0
15
ios – .sheet dismissed instantly when offered over DisclosureGroup


When a DisclosureGroup is proven inside a Checklist, it appears it is rather like a Group. In different phrases, it behaves like a group of views, reasonably than like a container.

On this context, making use of .sheet as modifier to DisclosureGroup (or to the wrapper view TestDisclosureGroup) is like including it individually to the label and to the content material. When the flag sheetIsPresented is ready to true, each sheets attempt to present concurrently, therefore the error.


One strategy to repair is to wrap the DisclosureGroup with a container and apply the .sheet to the container as a substitute. It is attention-grabbing to strive a ZStack as container:

ZStack {
    TestDisclosureGroup(disclosureGroupIsExpanded: $disclosureGroupIsExpanded) {
        sheetIsPresented = true
    }
}
.sheet(isPresented: $sheetIsPresented) {
    Textual content("Check")
}

When the DisclosureGroup is expanded, it appears like this:

enter image description here

This illustrates how the DisclosureGroup behaves like a bunch of views. It additionally demonstrates, that ZStack is not a good selection of container.

A VStack works a bit higher, however the animation is just not good.

So a greater strategy to repair is to maneuver the .sheet modifier to the Button contained in the DisclosureGroup, or to the Label (through the use of a special initializer for DisclosureGroup), or to the guardian Checklist. This in fact means altering the way in which you’re passing the bindings and closures round.

Right here is how it may be moved to the Checklist:

struct HomeView: View {
    @State personal var sheetIsPresented = false
    var physique: some View {
        Checklist {
            CompositedTestDisclosureGroup(sheetIsPresented: $sheetIsPresented)
        }
        .sheet(isPresented: $sheetIsPresented) {
            Textual content("Check")
        }
    }
 }

struct CompositedTestDisclosureGroup: View {
    @State personal var disclosureGroupIsExpanded = false
    @Binding var sheetIsPresented: Bool

    var physique: some View {
        TestDisclosureGroup(disclosureGroupIsExpanded: $disclosureGroupIsExpanded) {
            sheetIsPresented = true
        }
    }
}

LEAVE A REPLY

Please enter your comment!
Please enter your name here