6 June 2024 Leave a comment Tech-Help
With the release of iOS 13, Apple introduced a new default card presentation style for modal view controllers. While this new presentation can be visually appealing and useful in many scenarios, there are times when developers may want to revert to the traditional fullscreen presentation. This guide will walk you through the various methods to achieve fullscreen modal presentations in iOS 13 and later versions.
Setting Fullscreen Presentation Programmatically
The simplest way to ensure your modal view controller is presented in fullscreen is to set the modalPresentationStyle
property explicitly. Here’s how you can do it:
let vc = UIViewController()
vc.modalPresentationStyle = .fullScreen // or .overFullScreen for transparency
self.present(vc, animated: true, completion: nil)
Using Storyboard Segues
If you are using storyboards, you can set the presentation style directly in Interface Builder:
- Select the segue in your storyboard.
- In the Attributes Inspector, change the Presentation property to Full Screen.
Overriding the Present Method
For a more global approach, you can override the present(_:animated:completion:)
method in a base view controller:
class BaseViewController: UIViewController {
override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
viewControllerToPresent.modalPresentationStyle = .fullScreen
super.present(viewControllerToPresent, animated: flag, completion: completion)
}
}
Using Extensions
Alternatively, you can create an extension to handle fullscreen presentations:
extension UIViewController {
func presentInFullScreen(_ viewController: UIViewController, animated: Bool, completion: (() -> Void)? = nil) {
viewController.modalPresentationStyle = .fullScreen
present(viewController, animated: animated, completion: completion)
}
}
Usage:
presentInFullScreen(viewController, animated: true)
Handling Navigation Controllers
If you are presenting a navigation controller modally, make sure to set the modalPresentationStyle
on the navigation controller itself:
let navigationController = UINavigationController(rootViewController: yourViewController)
navigationController.modalPresentationStyle = .fullScreen
self.present(navigationController, animated: true, completion: nil)
Objective-C Implementation
For Objective-C developers, the implementation is similar:
UIViewController *vc = [UIViewController new];
vc.modalPresentationStyle = UIModalPresentationFullScreen;
[self presentViewController:vc animated:YES completion:nil];
Conclusion
By following these methods, you can ensure that your modal view controllers are presented in fullscreen mode, providing a consistent user experience across your iOS application. Each method has its use case, and you can choose the one that best fits your project’s needs.
For mobile developers who want to streamline their testing process, consider using Repeato. Repeato is a no-code test automation tool for iOS and Android that leverages computer vision and AI to create, run, and maintain automated tests quickly. This tool allows developers to focus on creating great products rather than spending time on testing, and it enables non-technical colleagues or QAs to handle test automation tasks effectively.