iOS Adaptive Presentation Controller

If you want to change the presented view controller’s modelPresentationStyle according to size class, here ‘s a guideline for you.

My setup is basic: I have a view controller and it has a button on it. When user taps on the button a new view controller is presented.

First you need to set presented view controller’s presentation controller delegate and then you need to implement adaptivePresentationStyle function. Show me code:

import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func buttonTapped(_ sender: UIButton) {
let vc = DetailViewController()
vc.presentationController?.delegate = self
self.present(vc, animated: true, completion: nil)
}
}
extension ViewController: UIAdaptivePresentationControllerDelegate {
func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
if traitCollection.verticalSizeClass == .compact {
return .overFullScreen
} else {
return .automatic
}
}
}

Here’s the demo: Modal presentation style changes between full screen and automatic.

Leave a comment