0

I created a view similar to the code I attached. It uses offset and a boolean to toggle the view up and down with either a button or a tap on outside space. HOWEVER, I been trying to also dismiss by dragging down. I tried many ways but apparently don't know enough about gestures to figure it out. I tried copying code from others but it hasn't worked out. I'd like it to behave the way Instagram's hamburger menu works in the users view. I attached some gifs of how mine works and how instagrams works.

enter image description here enter image description here

struct View1: View {
    
    @EnvironmentObject var viewModel : ViewModel
    
    var body: some View {
        
        ZStack {
            
            
            VStack {
                
                
                Text("Press Me")

                Spacer()
                
            }.padding(.top,50)

                .onTapGesture {

                    withAnimation(.default) {

                        viewModel.offsetView.toggle()
                    }

                }
            
            
        } //Z
        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
        .overlay(
            
            View2()
            
        )
        
    }
    
}

    struct View2: View {
    
    @EnvironmentObject var viewModel : ViewModel
    let array = ["ITEM 1", "ITEM 2", "ITEM 3", "ITEM 4", "ITEM 5", "ITEM 6"]
    let screenH = UIScreen.main.bounds.height
    let screenW = UIScreen.main.bounds.width
    
    
    var body: some View {
        
        VStack {
            
            ForEach (self.array, id: \.self) { array in
                
                Text(array)
                    .padding(.vertical)
                
                Divider()
                
            }
            
            Spacer()

        } //V
        .background(.bar)
        .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .leading)
        .offset(y: viewModel.offsetView ? screenH/2.2:screenH)
        .environmentObject(ViewModel())
    }

}

struct View1_Previews: PreviewProvider {
    static var previews: some View {

        View1()
            .environmentObject(ViewModel())
    }

}

    
class ViewModel: ObservableObject {
    
    @Published var offsetView = false
}
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • Try using a sheet instead of overlaying a different view. https://www.hackingwithswift.com/quick-start/swiftui/how-to-present-a-new-view-using-sheets – WilliamD47 Dec 17 '21 at 10:09
  • Can I control the height of the view. I don't need it to come up all the way like sheets does. Just half way like my example. – TheManOfSteell Dec 18 '21 at 04:17
  • Does this answer your question? [iOS 15: using UISheetPresentationController in SwiftUI](https://stackoverflow.com/questions/67982691/ios-15-using-uisheetpresentationcontroller-in-swiftui) – lorem ipsum Dec 23 '21 at 23:40

0 Answers0