— UIKit, Modal View Controllers, iOS — 1 min read
Fullscreen modals are a common UI element in iOS apps. They temporarily take over the entire screen, requiring the user to interact with them before returning to the underlying view. This article explores two methods for presenting fullscreen modals in UIKit:
modalPresentationStyle
to .fullScreen
This is the simplest approach for achieving a fullscreen modal. Here's how it works:
modalPresentationStyle
property of the modal view controller to .fullScreen
before presenting it.1let modalViewController = MyModalViewController()2modalViewController.modalPresentationStyle = .fullScreen3present(modalViewController, animated: true, completion: nil)
Remember: Even with this setting, ensure your modal view controller's view itself expands to fill the entire screen. You can achieve this by setting its frame to the screen bounds in viewDidLoad
or using Auto Layout constraints.
In iOS 13 and later, the default presentation style for modals is .automatic
. This might not always result in a fullscreen experience, especially when using a navigation controller. Here's how to ensure a fullscreen modal even with navigation:
1let modalViewController = MyModalViewController()2let navigationController = UINavigationController(rootViewController: modalViewController)3navigationController.modalPresentationStyle = .fullScreen4present(navigationController, animated: true, completion: nil)
By wrapping the modal view controller in a navigation controller and setting the modalPresentationStyle
of the navigation controller to .fullScreen
, you guarantee a fullscreen presentation on all supported iOS versions.
Some Additional Considerations: