UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Navigating inside a sheet with height animation

Forums > SwiftUI

Is it possible to navigate inside a sheet view and animate its height to adapt to the new sheet height?

Basically what I am looking for is this effect: <img src="https://i.ibb.co/hWH4gnJ/RPReplay-Final1667845678-MP4.gif" alt="RPReplay-Final1667845678-MP4" border="0">

Not sure if this is possible without creating a custom view, and just using the native sheet component from SwiftUI.

I've been trying different approaches and I can't get it to work. If I use isPresented, the view reloads and the sheet panel pops up or down to the new height. If I use item the panel gets dismissed and then reopens again with the new height.

Thanks! Fran

1      

bump! Any idea if this is possible??

1      

Below code does not auto adjust , but if you know how much content you need to show , may be can be useful


import SwiftUI

struct DismissingView1: View {
    @Environment(\.presentationMode) var presentationMode

    var body: some View {
        Button("Dismiss Me") {
            presentationMode.wrappedValue.dismiss()
        }
    }
}

struct DismissingView2: View {
    @Environment(\.presentationMode) var presentationMode

    var body: some View {
        Button("Dismiss Me Now") {
            presentationMode.wrappedValue.dismiss()
        }
    }
}

struct CardView: View {
    @State private var showingDetail = false
    @State private var showingDetail2 = false
    var body: some View {
        Button("Show Detail") {
                   showingDetail = true
               }
               .sheet(isPresented: $showingDetail) {
                   DismissingView1()
                       .presentationDetents([.height(100), .fraction(20), .medium, .large])
                                          .presentationDragIndicator(.hidden) 
                                          .edgesIgnoringSafeArea(.all)
               }

        Button("Show Sheet 2") {
                   showingDetail2 = true
               }
        .sheet(isPresented: $showingDetail2) {
            DismissingView2()
                .presentationDetents([.height(300), .fraction(20), .medium, .large])
                .presentationDragIndicator(.hidden) 
                .edgesIgnoringSafeArea(.all)
        }

    }

}

1      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.