lucaszischka / BottomSheet

A sliding Sheet from the bottom of the Screen with 3 States build with SwiftUI.
MIT License
1.03k stars 138 forks source link

The child view onAppear is not fired consistently with .bottomSheet but it is consistent when using .sheet. #160

Open gjmoyer opened 5 months ago

gjmoyer commented 5 months ago

Describe the bug The child view onAppear is not fired consistently with .bottomSheet but it is consistent when using .sheet.

Minimal reproduce-able code

struct TabbedViewWithBottomSheet: View {
    @State var bottomSheetPosition: BottomSheetPosition = .hidden
    @State var sheetContent: any View = EmptyView()
    @State var showSheet: Bool = false

    var body: some View {
        TabView {
            VStack {
                Button("show1") {
                    sheetContent = ChildView1(name: "Show1 sheet1")
                    bottomSheetPosition = .relativeTop(1)
                }
                Button("show2") {
                    sheetContent = ChildView1(name: "Show2 sheet2")
                    bottomSheetPosition = .relativeTop(1)
                }
                Button("show3") {
                    sheetContent = ChildView1(name: "Show3 sheet3")
                    showSheet.toggle()
                }
                Button("show4") {
                    sheetContent = ChildView1(name: "Show4 sheet4")
                    showSheet.toggle()
                }
            }
            .bottomSheet(
                bottomSheetPosition: self.$bottomSheetPosition,
                switchablePositions: [.relativeTop(1), .hidden]
            ) {
                AnyView(sheetContent)
            }
            .enableSwipeToDismiss()
            .showCloseButton()
            .sheetWidth(BottomSheetWidth.relative(1))
            .tabItem {
                Image(systemName: "magnifyingglass")
                Text("Search")
            }
            .tag(0)

            Text("Lists content")
                .tabItem {
                    Image(systemName: "list.dash")
                    Text("Lists")
                }
                .tag(1)
        }
        .sheet(isPresented: $showSheet) {
            AnyView(sheetContent)
        }

    }
}

struct ChildView1: View {
    var name: String

    var body: some View {
        VStack {
            Text("Hello, \(self.name)!")
        }
        .onAppear {
            print("ChildView1.onAppear \(self.name)")
        }
    }
}

Expected behavior I expect the onAppear to fire in the ChildView every time the bottom view is not hidden. If I have logic that fires in the onAppear to populate the view from an API this doesn't fire and the previous view content is shown instead.

Target version