1

Modally presenting a ViewController on iPhone 6+ with a style of UIModalPresentationFormSheet opens differently according to orientation.

In portrait mode it seem like regular Modal (same as smaller iPhones). But in landscape mode it opens as form (similar to iPads).

How can I tell programmatically what state was actually used (anywhere with in the VC's life cycle).

AShavit
  • 416
  • 3
  • 10
  • can you used this one `modalTransitionStyle`. – SGDev Apr 20 '15 at 13:12
  • @SumitGarg modalTransitionStyle is displayed even on older iPhones when the VC covers the whole screen. You can check the correct answer below – AShavit Apr 20 '15 at 15:00

2 Answers2

2

The controller displays as a form sheet when the size class for the controller has regular width.

So on an iPhone 6+ in landscape, or on an iPad in any orientation the horizontal size class is regular and the form is displayed less than full screen width.

You can test for this in a controller using:

if (self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassRegular) {
  // ... Its showing as per the form specification
}
else{
  // ... Its showing as a modal full screen.
}

Replace self with the variable for a controller if being called from somewhere else.

This also covers the case where you might use a popover, since when you use a popover on an iPad the size class changes to compact within the popover itself.

Rory McKinnel
  • 7,936
  • 2
  • 17
  • 28
  • 2
    Thank! It works. I actually used the size classes to check if the VC is compact and the presenting is regular, since if both are compact means that it was presented modally and not as for. `if (self.traitCollection.horizontalSizeClass < self.presentingViewController.traitCollection.horizontalSizeClass) { // ... Its showing as per the form specification } else{ // ... Its showing as a modal full screen. }` – AShavit Apr 20 '15 at 14:59
2

A clean Swift solution is below. A UIViewController extension that adds the isBeingPresentedInFormSheet method.

extension UIViewController {
    func isBeingPresentedInFormSheet() -> Bool {
        if let presentingViewController = presentingViewController {
            return traitCollection.horizontalSizeClass == .Compact && presentingViewController.traitCollection.horizontalSizeClass == .Regular
        }
        return false
    }
}

This method returns true if the view controller is currently being displayed in a Form Sheet.

In my experience this occurs when

modalPresentationStyle = .FormSheet

and the device is an iPad or iPhone 6 Plus in landscape orientation.

dfmuir
  • 2,048
  • 1
  • 17
  • 14