Search code examples
iosswiftuipangesturerecognizeruiwindow

Should I add bottom sheet as a subview to current view controller or push a UIWindow with the subView added?


I want to implement a bottom sheet as per the image. The bottom will have a pan gesture to pull up and down

What I am confused about is :

  • Should I add a view to the existing view controller and add pan gesture to that view to pull up and down.
  • Should create a new window and add bottom sheet as a subview and push that window.

If, I choose the second option how would I update window on pan?

enter image description here


Solution

  • You should not create a new UIWindow. Generally an iOS app has just 1 window. The two best options I see are:

    1. You can add a subview and gesture recognisers to implement pulling. That means that all the logic will probably be within the parent UIViewController. If there is not much logic, and the sheet does not have to be reusable, this is a good option.

    2. If there is more logic inside the sheet, or the sheet has the be reusable, then it would be better to create a separate UIViewController for this. You can then present this using a custom animation and a presentation controller. Look into UIViewControllerAnimatedTransitioning and UIPresentationController for this.

    I have used the second option before. A UIPresentationController take care of positioning the sheet view on the bottom of screen. It also dims the background, and attaches gesture recognisers to handle pulling and dismissing.

    You can use this tutorial to get started with a UIPresentationController. You can also take the code from there and adapt it to your needs: https://www.raywenderlich.com/915-uipresentationcontroller-tutorial-getting-started