Group Group Group Group Group Group Group Group Group

raywenderlich.com Forums

Video Tutorial: Adaptive Layout Part 7: Adaptive Presentation

Learn about Adaptive Presentation and how a view controller can control its own presentation style at run time based on the current trait collection or any other conditions.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3187-adaptive-layout/lessons/8

There’s a bug in the ChallengeFinished project when you rotate a phone to horizontal while the popup is presented. You will get a full screen view with no Done button.

You’re right. It seems when the device is rotated and the framework calls adaptivePresentationStyleForPresentationController(_:traitCollection:), the trait collection is the new one, with the compact height. But when it calls presentationController(_:viewControllerForAdaptivePresentationStyle:), the controller still has the old trait collection. I was able to solve this by adding a property to the view controller:

var presentingTraitCollection: UITraitCollection!

Then saving the last trait collection in adaptivePresentationStyleForPresentationController(_:traitCollection:):

func adaptivePresentationStyleForPresentationController(controller: UIPresentationController,
    traitCollection: UITraitCollection) -> UIModalPresentationStyle {
  presentingTraitCollection = traitCollection 
  if traitCollection.verticalSizeClass == .Regular {
    return .None
  } else {
    return .FullScreen
  }
}

Then changing this line in presentationController(_:viewControllerForAdaptivePresentationStyle:) from :

if adaptivePresentationStyleForPresentationController(controller, traitCollection: controller.traitCollection) == .None {

to:

if adaptivePresentationStyleForPresentationController(controller, traitCollection: presentingTraitCollection) == .None {