Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SwiftUI: How to pass a View as a parameter into another View to get Bottom Sheet?

Tags:

ios

swift

swiftui

I'm trying to create a View for the bottom sheet. It will have some view with a button when we tap on the button a bottom sheet has to appear.

I can modify the bottom sheet's colour, height, corner radius, how much the background view has to blur.

struct BottomSheetView: View {
    @Binding var showBottomSheet: Bool
    @Binding var bgColor: Color
    @Binding var cornerRadius: CGFloat
    @Binding var bottomSheetRatio: CGFloat
    @Binding var blurPoint: CGFloat

    var body: some View {
        GeometryReader { geometry in
            ZStack {
                VStack {
                    Button(action: {
                        self.showBottomSheet.toggle()
                    }){
                        Text("click here")
                        .padding()
                    }
                    Spacer()
                }
                .frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
                .blur(radius: self.showBottomSheet ? self.blurPoint : 0)
                .onTapGesture {
                    if self.showBottomSheet {
                        self.showBottomSheet = false
                    }
                }
                Rectangle()
                    .fill(self.bgColor)
                    .cornerRadius(self.cornerRadius)
                    .offset(y: self.showBottomSheet ? geometry.size.height * self.bottomSheetRatio : geometry.size.height + 200)
                    .animation(.spring())
            }
        }
    }
}

In the above code

VStack {
    Button(action: {
        self.showBottomSheet.toggle()
    }){
        Text("click here")
             .padding()
    }
    Spacer()
}

This VStack should replace with some other View, from that View I want to pass the Binding values.

Is there a way to achieve that? Thanks in advance.

like image 975
Azhagusundaram Tamil Avatar asked Oct 22 '25 04:10

Azhagusundaram Tamil


1 Answers

I have achieved the requirement with ViewModifier

First I have created a ViewModifier struct. which will have all the required values in init()

struct BottomSheetModifier: ViewModifier {
    var showBottomSheet: Bool
    var blurPoint: CGFloat
    var bgColor: Color
    var cornerRadius: CGFloat
    var bottomSheetRatio: CGFloat

    init(showBottomSheet: Bool, blurPoint: CGFloat, bgColor: Color, cornerRadius: CGFloat, bottomSheetRatio: CGFloat) {
        self.showBottomSheet = showBottomSheet
        self.blurPoint = blurPoint
        self.bgColor = bgColor
        self.cornerRadius = cornerRadius
        self.bottomSheetRatio = bottomSheetRatio
    }

    func body(content: Content) -> some View {
        GeometryReader { geometry in
            ZStack {
                content
                    .blur(radius: self.showBottomSheet ? self.blurPoint : 0)

                RoundedRectangle(cornerRadius: self.cornerRadius)
                    .fill(self.bgColor)
                    .offset(y: self.showBottomSheet ? geometry.size.height * self.bottomSheetRatio : geometry.size.height + 200)
                    .animation(.spring())
            }

        }
    }
}

Second I've created an extension for View which will have a function with all the values as arguments and initialized the modifier in it.

extension View {
    func bottomSheet(showBottomSheet: Bool, blurPoint: CGFloat, bgColor: Color, cornerRadius: CGFloat, bottomSheetRatio: CGFloat) -> some View {
        modifier(BottomSheetModifier(showBottomSheet: showBottomSheet, blurPoint: blurPoint, bgColor: bgColor, cornerRadius: cornerRadius, bottomSheetRatio: bottomSheetRatio))
    }
}

Third I've created a View. In that I have added some elements and simply call the modifier here with my required bottomsheet size, colour and other values.

struct DemoBottomSheetView: View {
    @Binding var showBottomSheet: Bool

    var body: some View {
        VStack(spacing: 20) {
            Text("welcome to bottom sheet")
            Button(action: {
                self.showBottomSheet.toggle()
            }){
                Text("Click Me")
                    .padding()
                    .background(Color.blue)
                    .foregroundColor(.white)
                    .cornerRadius(10)
            }
            Spacer()
        }
        .onTapGesture {
            if self.showBottomSheet {
                self.showBottomSheet = false
            }
        }
        .bottomSheet(showBottomSheet: self.showBottomSheet, blurPoint: 4, bgColor: .red, cornerRadius: 25, bottomSheetRatio: 0.5)
    }
}

Final output

Final output

like image 106
Azhagusundaram Tamil Avatar answered Oct 23 '25 18:10

Azhagusundaram Tamil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!