【问题标题】:How can I tell programmatically if a ViewController presented UIModalPresentationFormSheet opened on full screen or not?如何以编程方式判断 ViewController 是否呈现 UIModalPresentationFormSheet 全屏打开?
【发布时间】:2015-04-20 13:06:03
【问题描述】:

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

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

我如何以编程方式判断实际使用的状态(VC 生命周期中的任何地方)。

【问题讨论】:

  • 你能用这个吗modalTransitionStyle.
  • @SumitGarg modalTransitionStyle 在 VC 覆盖整个屏幕时甚至在较旧的 iPhone 上也会显示。您可以在下方查看正确答案

标签: ios iphone-6-plus uimodalpresentationstyle


【解决方案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 上使用弹出框时,尺寸等级会在弹出框本身内更改为紧凑。

【讨论】:

  • 谢谢!有用。实际上,我使用大小类来检查 VC 是否紧凑并且呈现是否规则,因为如果两者都是紧凑的,则意味着它是模态呈现的,而不是模态呈现的。 if (self.traitCollection.horizontalSizeClass < self.presentingViewController.traitCollection.horizontalSizeClass) { // ... Its showing as per the form specification } else{ // ... Its showing as a modal full screen. }
【解决方案2】:

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

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。

【讨论】:

    猜你喜欢
    • 2019-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-07
    • 2015-02-11
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多