Search code examples
swiftswiftuiios16.4

Is it possible to get the height of a sheet when dragging it up in SwiftUI iOS 16.4?


iOS 16.4 added features that allow us to specify presentation detents. For example:

@State var isSheetPresented: Bool = false
@State var detent: PresentationDetent = .height(200)

MyView()
.sheet(isPresented: $isSheetPresented) {
    MySecondView()
}
.presentationDetents([.height(200), .height(500)], selection: $detent)

Now detent has two settings, 200 and 500, and it switches when the user swipes up or down on the sheet. Is it possible in iOS 16.4 to determine the height of the sheet during this swiping process? During the swipe, the height goes from 200 to 500, and if I wanted to base some animations of this value, could I do that?


Solution

  • Wrap MySecondView() in a Geometry Reader (either in the sheet closure or in its actual struct). Then its proxy's height property will give you the value you want.

    MyView()
    .sheet(isPresented: $isSheetPresented) {
        Geometry Reader { proxy in
            MySecondView()
        }
        // access proxy.size.height here
    }