Adapting UISheetPresentationController for SwiftUI

Caleb Friden
Oct 1, 2021

--

UISheetPresentationController was recently released for iOS 15 and finally gives us a 1st party component for the Apple map style bottom-sheet seen in many apps. Unfortunately, iOS 15 does not ship with an equivalent component for SwiftUI, but with a little UIViewControllerRepresentable magic, we can get it working in SwiftUI:

This approach works by creating wrapping our SwiftUI views in UIHostingController instances, which can leverage the new UISheetPresentationController APIs. The hosting controllers are subclassed so that we can capture the presentation and dismissal events to keep the SwiftUI bindings up-to-date with the state of the view. We then take the wrapped the controllers in return them in a UIViewControllerRepresentable SwiftUI view. Finally, we bundle the whole package up into a view modifier to give us a SwiftUI-style API for creating our new sheet. The full source code can be viewed here:

--

--