Member-only story
Presenting Sheets Of Various Heights In SwiftUI
A new view modifier makes the job easy.
Back in time I had written a post about sheets in SwiftUI, discussing how to present and work with them. Although the content of that tutorial remains valid, new interesting additions to sheets were introduced in WWDC 2022 that definitely deserve a new post to talk about them.
Given that a sheet is a system provided view, the customization options available at our disposal are limited, or at least, used to be when it comes to the sheet’s height. Starting from iOS 16, we gain the control of it in SwiftUI, becoming able to present modal content not only in full, but also in half, even in custom height. The cherry on the cake is the option to modify the height on the fly programmatically if necessary.
Let’s get familiar with all that by going through a practical example, where we’ll meet the few bits of code needed to change a sheet’s height.
Presenting a sheet
We’ll start off by declaring the following state property in a brand new SwiftUI view; its purpose is to control the presented state of the sheet: