user19402781
user19402781

Reputation: 63

Bottom Sheet not automatically getting dismissed when navigating to another view in SwfitUI

I am using bottom sheets in SwiftUI and when I navigate to another screen the bottom sheet is not getting dismissed. Does anyone know if this is supposed to be the default behaviour for bottom sheets. Or do I have to manually toggle the bool controlling the bottom sheet as I navigiate to another view (is there an easy way to do this as this seems tedious). The basic outline of the code I have is as follows

NavigationStack {
   ZStack {
      // displayed views and navigation links to navigate to other views
   }
   .bottomSheet(presentationDetents: [.height(70), .medium, .large], isPresented: .constant(true), sheetCornerRadius: 20) {
            OrderScrollView()
    } onDismiss: {}
}

Where bottomSheet is a custom bottomSheet defined as such

@viewBuilder
func bottomSheet<Content: View> ( // all parameters )->some View {
   self.sheet(isPresented: isPresented) {
            onDismiss()
        } content: {
            content()
                .presentationDetents(presentationDetents)
                .presentationDragIndicator(dragIndicatior)
                .interactiveDismissDisabled(interactiveDismissDisabled)
                .onAppear {
                    guard let window = UIApplication.shared.connectedScenes.first as?
                            UIWindowScene else {
                        return
                    }
                
                    if let viewController = window.windows.first?.rootViewController?.presentedViewController,
                       let sheet = viewController.presentationController as? UISheetPresentationController {
                        
                        if isTransparentBackGround {
                            controller.view.backgroundColor = .clear
                        }
                        
                        controller.presentingViewController?.view.tintAdjustmentMode = .normal
                        sheet.largestUndimmedDetentIdentifier = largestUndimmedIdentifier
                        sheet.preferredCornerRadius = sheetCornerRadius
                    } else {
                        print("Bottom Sheet is attached to no View/Controller.")
                    }
                }
        }
    }

I used .constant(true) as I assumed that on navigation the bottom sheet would by default collapse. Any Advice is appreciate thanks.

I know that manually toggling a boolean whenever I go to or come back from navigation links could be a way to solve this but seems very tedious just to control the state of the bottom sheet.

Upvotes: 0

Views: 873

Answers (1)

Pedro Henrique
Pedro Henrique

Reputation: 11

Maybe the better way is to create a state variable to control your bottom sheet. In this way, it's impossible to dismiss because your constant is set to true and constant never change values. So try using state var to control the bottom sheet instead of this.

Upvotes: 0

Related Questions