Skip to content

Instantly share code, notes, and snippets.

@konnnn
Last active April 15, 2022 21:08
Show Gist options
  • Save konnnn/1f280f256a0c21a4d5119424c258722f to your computer and use it in GitHub Desktop.
Save konnnn/1f280f256a0c21a4d5119424c258722f to your computer and use it in GitHub Desktop.
Present a ViewController on Half screen
// iOS 15 and above
func presentSecondViewController() {
guard let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as? SecondViewController else { return }
secondViewController.providesPresentationContextTransitionStyle = true
secondViewController.definesPresentationContext = true
secondViewController.modalPresentationStyle = .formSheet // The presentation style for modal view controllers
secondViewController.isModalInPresentation = true // Cancels dismiss the viewController by swiping down
secondViewController.modalTransitionStyle = .coverVertical // The transition style to use when presenting the view controller
if let presentationViewController = secondViewController.presentationController as? UISheetPresentationController {
presentationViewController.detents = [.medium()] // change to [.medium(), .large()] for a half *and* full screen sheet
presentationViewController.preferredCornerRadius = 42.0
}
self.present(secondViewController, animated: true, completion: nil)
}
@konnnn
Copy link
Author

konnnn commented Apr 15, 2022

Half-screen sheet / .detents = [.medium()]
https://i.stack.imgur.com/Siycc.gif

Half and full-screen sheet / .detents = [.medium(), .large()]
https://i.stack.imgur.com/DHGCk.gif

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment