Trevor
Trevor

Reputation: 843

SwiftUI .sheet() disables background interaction

Background: SwiftUI 4.0 (iOS 16) brings a ton of cool new things to use, one is the new .sheet() with .presentationDetents(), this basically allows you to create a resizeable sheet like the one in apple maps. Bottom sheet tutorial

Problem: You cannot interact with the background of the .sheet(). See image below: screenshot While the sheet is open (in any position) you cannot move the map around in the background. How can I enable interaction with the background while using presentation detents?

Code:

struct CreateStoryView: View {
    @EnvironmentObject var csvm: CreateStoryViewModel
    @Binding var currentUserData: UserModel
    @Binding var showCreateStoryView: Bool
    private let topBarHeight: CGFloat = 50
    private let viewPadding: CGFloat = 15
    
    var body: some View {
        GeometryReader { geo in
            let mapFrame = CGSize(width: geo.size.width, height: geo.size.height + geo.safeAreaInsets.magnitude)
            
            ZStack(alignment: .top) {
                CreateFenceMapView(geoSize: mapFrame, viewPadding: self.viewPadding)
                CreateStoryNavBar(showCreateStoryView: $showCreateStoryView, topBarHeight: self.topBarHeight, viewPadding: self.viewPadding, geoSize: geo.size)
            }
            .sheet(isPresented: $showCreateStoryView, content: {
                if #available(iOS 16.0, *) {
                    CreateStoryDetailsView(currentUserData: $currentUserData, geoSize: geo.size)
                        .presentationDetents([.fraction(0.25), .large])
                        .presentationDragIndicator(.visible)
                        .interactiveDismissDisabled()
                } else {
                    ZStack {
                        Color.theme.accentLight
                    }
                }
            })
        }
    }
}

Upvotes: 0

Views: 1690

Answers (1)

devin132
devin132

Reputation: 91

In IOS 16.4+ you can use PresentationBackgroundInteraction to enable background interaction when using a sheet.

For example:

var body: some View {
        YourCustomView()
            .sheet (isPresented: .constant(true)) {
                Text("custom view should still be enabled.")
                    .presentationDetents([.medium, .large])
                .presentationBackgroundInteraction(.enabled(upThrough: .medium))
            }
    }

See: https://developer.apple.com/documentation/swiftui/presentationbackgroundinteraction

Upvotes: 3

Related Questions