SwiftUI: Can't get the transition of a DetailView to a ZStack in the MainView to work

大憨熊 提交于 2021-02-04 06:20:10

问题


I can't find the answer to this anywhere, hopefully one of you can help me out.

I have a MainView with some content. And with the press of a button I want to open a DetailView. I am using a ZStack to layer the DetailView on the top, filling the screen.

But with the following code I can't get it to work. The DetailView does not have a transition when it inserts and it stops at removal. I have tried with and without setting the zIndex manually, and a custom assymetricalTransition. Couldn't get that to work. Any solutions?

//MainView
@State var showDetail: Bool = false

var body: some View {
    ZStack {
        VStack {
            Text("Hello MainWorld")
            Button(action: {
                withAnimation(.spring()) {
                    self.showDetail.toggle()
                }
            }) {
                Text("Show detail")
            }
        }


        if showDetail {
            ContentDetail(showDetail: $showDetail)
                .transition(.move(edge: .bottom))
        }
    }
    .edgesIgnoringSafeArea(.all)
}

And here is the DetailView:

//DetailView

@Binding var showDetail: Bool    

var body: some View {
    VStack (spacing: 25) {
        Text("Hello, DetailWorld!")

        Button(action: { withAnimation(.spring()) {
            self.showDetail.toggle()
            }

        }) {
            Text("Close")
        }
        .padding(.bottom, 50)


    }
    .frame(width: UIScreen.main.bounds.width,
           height: UIScreen.main.bounds.height)
    .background(Color.yellow)
    .edgesIgnoringSafeArea(.all)

}

The result of this code is this:

I'm running Xcode 11.4.1 so implicit animations doesn't seem to work either. Really stuck here, hope one of you can help me out! Thanks :)


回答1:


Here is a solution. Tested with Xcode 11.4 / iOS 13.4.

struct MainView: View {
    @State var showDetail: Bool = false

    var body: some View {
        ZStack {
            Color.clear // extend ZStack to all area
            VStack {
                Text("Hello MainWorld")
                Button(action: {
                        self.showDetail.toggle()
                }) {
                    Text("Show detail")
                }
            }

            if showDetail {
                DetailView(showDetail: $showDetail)
                    .transition(AnyTransition.move(edge: .bottom))
            }
        }
        .animation(Animation.spring())  // one animation to transitions
        .edgesIgnoringSafeArea(.all)
    }
}

struct DetailView: View {
    @Binding var showDetail: Bool

    var body: some View {
        VStack (spacing: 25) {
            Text("Hello, DetailWorld!")

            Button(action: {
                self.showDetail.toggle()
            }) {
                Text("Close")
            }
            .padding(.bottom, 50)


        }
        .frame(maxWidth: .infinity, maxHeight: .infinity) // fill in container
        .background(Color.yellow)
    }
}


来源:https://stackoverflow.com/questions/61445745/swiftui-cant-get-the-transition-of-a-detailview-to-a-zstack-in-the-mainview-to

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!