How to present sheets with UIKit using a UISheetPresentationController

July 05, 2021 Publish By : EXPERT APP DEVS 2 min read Viewed By : 973
UISheetPresentationController

UISheetPresentationController

  • WWDC 2021 introduced iOS 15 with many changes, including improvements to presenting sheets in UIKit with the new controller UISheetPresentationController. Before you present our view controller, we can configure its sheet presentation controller with the behavior and appearance you want for your sheet.
  • We can add different configuration sizes by defining detents in our presented view controller. Currently, there are two predefined sizes for medium and large presentations. 
  1. Medium 
  2. Large

1) Presenting a sheet in UIKit

  • Presenting sheets is as easy as using the present controller method :

class VC: UIViewController {

    override func viewDidLoad() {

        super.viewDidLoad()

    }

    @IBAction func openVC() {

        let sheetVC = SheetViewController(nibName: nil, bundle: nil)

        present(sheetVC, animated: true, completion: nil)

    }

}

2) Defining detents to allow different sheet sizes

  • We can add different size configurations to our presentation Controller by defining detents in our presented controller. There are two types Medium and Large .if let presentationVC = presentationController as? UISheetPresentationController {

    presentationVC.detents = [

        .medium(),

        .large()

    ]

  }

Screen Shots :- 

Presenting a sheet in UIKitDefining detents to allow different sheet sizes

 

3) Visible grabber 

  • To improve the user experience, it’s good to add a little indicator showing that the user can drag the sheet.
  • We can add grabber by adding single line in presented controller 

presentationVC.prefersGrabberVisible = true

For example :

if let presentationVC = presentationController as? UISheetPresentationController {

    presentationVC.detents = [

        .medium(),

        .large()

    ]

    presentationVC.prefersGrabberVisible = true

}

Screenshots:-

Visible grabber

 

Reference:-

Need a consultation?

Drop us a line! We are here to answer your questions 24/7.