【问题标题】:Detect if my controller is presented as popover检测我的控制器是否显示为弹出框
【发布时间】:2019-08-16 16:53:13
【问题描述】:

使用 Xcode 10.1 和 Swift 4.2.1、iOS 12

我在导航控制器中嵌入了一个表格视图。在右上角有一个条形按钮和一个从该按钮到保存静态表格视图的导航控制器的 segue(弹出框),这里的单元格与其他表格视图有进一步的 segue。

在 iPhone 上,静态表格视图显示为全屏模式,在 iPad 上,它是一个弹出框,到目前为止还可以。 我已经在情节提要中设置了所有内容,不知道这是否是原因,但现在我正在努力检查视图是弹出窗口还是全屏模式。

我试过了:

print("\(presentationController)")
if presentationController is UIPopoverPresentationController {
    // Do something
}

但这不起作用 -> print("\(presentationController)") 在 iPhone 和 iPad 上给我 Optional(<_UIFullscreenPresentationController: 0x7fd00ad45770>)

我也尝试过:

if popoverPresentationController != nil {
    print("popover")
}

但如果在 iPhone 或 iPad 上启动,则会打印弹出框。

我在这里做错了什么还是我错过了什么?

【问题讨论】:

  • @RajSharma 我已经读过那个了。最初的问题是在 3 1/2 年前写的,iOS8。官方标记的答案只是一个没有任何代码的小描述,我顺便尝试了一下,但没能附上。此外,我发现的其他答案甚至更老并且在目标 c 中(以前从未使用/编写过目标 c)。所以我猜在 iOS 12 中与 iOS8 相比是“一些”变化和更新,这可能更容易检查是否在弹出窗口中。
  • @Leo,下面的答案能解决这个问题吗?

标签: ios swift xcode storyboard uipopovercontroller


【解决方案1】:

使用presentedViewController的“UIModalPresentationStyle”来检测presentation style是全屏还是popover。

您可以在此处找到更多详细信息:https://developer.apple.com/documentation/uikit/uimodalpresentationstyle

【讨论】:

  • 我很快添加了print("ModalPresentationStyle: \(presentedViewController?.modalPresentationStyle)"),但在这两种情况下都是nil
  • @Leo,能否请您将代码分享到 GitHub,以便我们查看并解决此问题?
  • 我试过这个但发现它是.popover不管
【解决方案2】:

目前我正在使用这个:

if (popoverPresentationController?.arrowDirection != UIPopoverArrowDirection.unknown) {
    tableView.sectionHeaderHeight = CGFloat.leastNormalMagnitude
    tableView.sectionFooterHeight = CGFloat.leastNormalMagnitude
}

这很简单,而且很有效。如果有人真的有可能找出“如果是弹出窗口”,非常欢迎您在这里发布。

【讨论】:

    【解决方案3】:

    您可以在呈现的 ViewController 中检查这一点

    if self.modalPresentationStyle == .popover {
         print("Popover presentation")
    }
    

    如果呈现的视图控制器包含在导航控制器中,则使用此

    if self.navigationController?.modalPresentationStyle == .popover {
         print("Popover presentation")
    }
    

    【讨论】:

      猜你喜欢
      • 2014-03-21
      • 1970-01-01
      • 1970-01-01
      • 2019-11-13
      • 2021-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多