iOS Swift UIKit

Presenting sheets with UISheetPresentationController

Another welcomed change what WWDC’21 brought us was UISheetPresentationController. The sheet presentation controller makes it extremely easy to present sheets like the ones which just take a half of the screen. It allows us to configure the sheet size with detents where there are currently two of them: medium and large. The medium detent means that the sheet takes about the half of the screen height, and the large detent means that the full screen is covered with the sheet. Additionally, we can configure some other visual aspects like corner radius and if the grabber should be visible. The detents property is an array, and we can set both medium and large to it, which enables to dynamically change the size either by dragging or controlling the size programmatically by setting the selectedDetentIdentifier property. And if we would like to animate the change, then the property needs to be set within the animateChanges closure.

let sheetViewController = SheetViewController()
if let sheetController = sheetViewController.sheetPresentationController {
sheetController.detents = [.medium(), .large()]
sheetController.preferredCornerRadius = 4
sheetController.prefersGrabberVisible = true
present(sheetViewController, animated: true, completion: nil)
view raw Sheet.swift hosted with ❤ by GitHub

Example Project

SheetPresentationController (Xcode 13.1)

If this was helpful, please let me know on Mastodon@toomasvahter or Twitter @toomasvahter. Feel free to subscribe to RSS feed. Thank you for reading.