如何以编程方式告诉ViewController是否在全屏幕上打开了UIModalPresentationFormSheet?

时间:2015-04-20 13:06:04

标签: ios iphone-6-plus uimodalpresentationstyle

在iPhone 6+上以UIModalPresentationFormSheet样式模式呈现ViewController会根据方向以不同方式打开。

在纵向模式下,它看起来像普通模态(与较小的iPhone相同)。 但在横向模式下,它会以形式打开(类似于iPad)。

如何以编程方式告诉实际使用的状态(VC生命周期中的任何位置)。

2 个答案:

答案 0 :(得分:2)

下面是一个干净的Swift解决方案。一个UIViewController扩展,它添加了isBeingPresentedInFormSheet方法。

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

如果视图控制器当前显示在表单中,则此方法返回true。

根据我的经验,这发生在

modalPresentationStyle = .FormSheet

该设备是横向的iPad或iPhone 6 Plus。

答案 1 :(得分:1)

当控制器的尺寸类具有常规宽度时,控制器显示为表单。

因此,在横向iPhone 6+或任何方向的iPad上,水平尺寸等级都是常规尺寸,而且表格显示的宽度不足全屏。

您可以使用以下方法在控制器中测试:

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

如果从其他地方调用,则将self替换为控制器的变量。

这也涵盖了您可能使用弹出窗口的情况,因为当您在iPad上使用弹出框时,大小等级会在弹出窗口中变为紧凑。