【问题标题】:UIModalPresentationPopover for iPhone 6 Plus in landscape doesn't display popover横向 iPhone 6 Plus 的 UIModalPresentationPopover 不显示弹出框
【发布时间】:2015-08-03 09:17:58
【问题描述】:

我希望始终在所有设备和所有方向的弹出窗口中显示ViewController。我试图通过采用UIPopoverPresentationControllerDelegate 并设置sourceViewsourceRect 来实现这一点。

这适用于所有设备和方向,除了 iPhone 6 Plus 横向。在这种情况下,视图控制器会在表单中从屏幕底部向上滑动。我怎样才能防止它总是出现在弹出窗口中?

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
let popoverPresentationController = segue.destinationViewController.popoverPresentationController
popoverPresentationController?.delegate = self
popoverPresentationController?.sourceView = self.titleLabel!.superview
popoverPresentationController?.sourceRect = self.titleLabel!.frame }

func adaptivePresentationStyleForPresentationController(controller: UIPresentationController) -> UIModalPresentationStyle {
return UIModalPresentationStyle.None }

所有设备均在 iOS 8.2 或更高版本

【问题讨论】:

    标签: ios iphone ios8 iphone-6-plus uimodalpresentationstyle


    【解决方案1】:

    实现UIAdaptivePresentationControllerDelegate的新adaptivePresentationStyleForPresentationController:traitCollection:方法:

    - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller traitCollection:(UITraitCollection *)traitCollection {
        // This method is called in iOS 8.3 or later regardless of trait collection, in which case use the original presentation style (UIModalPresentationNone signals no adaptation)
        return UIModalPresentationNone;
    }
    

    UIModalPresentationNone 告诉演示控制器使用原始演示样式,在您的情况下将显示一个弹出框。

    【讨论】:

    • 好电话!我忘记了 8.3 的委托方法发生了变化。
    • @PetahChristian 谢谢!是的,这是一个非常安静的变化,除了 API 差异之外似乎没有记录。
    • @Joshua 是的!我确实实现了 - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *)controller { return UIModalPresentationNone; } 但新的 API 解决了我的问题,谢谢先生!
    • 出于某种原因,我读了三遍才终于看到该委托方法的 :traitCollection 部分。我最初认为,“是的,我已经实现了”,因为较短的版本适用于其他设备。谢谢! ...我一直在苦苦思索为什么它不适用于 6 岁以上。这总是最简单的事情。
    • 为什么苹果要为横向的 6/7 Plus 做这个特定的改变?如果这是您要求的,为什么不只显示一个弹出框?
    【解决方案2】:

    在 Swift 3 中,如果您实现了原始的 adaptivePresentationStyle 方法,只需添加以下代码即可:

    func adaptivePresentationStyle(for controller: UIPresentationController, traitCollection: UITraitCollection) -> UIModalPresentationStyle {
        return adaptivePresentationStyle(for: controller)
    }
    

    【讨论】:

      【解决方案3】:

      Apple 根据其尺寸等级设计了 iPhone 6 Plus 演示文稿以采用这种方式。

      为了防止 iPhone 6 Plus 上的模态显示,您必须覆盖特征集合(水平尺寸)。

      您应该能够为演示控制器设置overrideTraitCollection 属性:

      presentedVC.presentationController.overrideTraitCollection = [UITraitCollection traitCollectionWithHorizontalSizeClass:UIUserInterfaceSizeClassCompact];
      

      (对不起Objective C!我还没学过Swift。)

      【讨论】:

      • 我很抱歉,但它对我不起作用,我无法覆盖 traitCollection,在 iPhone 6 上,“意思是”弹出窗口仍然显示为页面表,并且仅景观
      • 虽然很感兴趣,但我今天早上没有时间调查这个。我可以在这个问题上提供赏金以引起人们的注意。 :)
      • 我看到了,谢谢 :) traitCollection 覆盖似乎是个好主意,但似乎被忽略了。
      • 好吧,当您在设置 modalPresentationStyle = UIModalPresentationPopover 之前添加此行时,它会覆盖特征集合但是,我认为 UIPopoverPresentationController 不知道此更改并且仍然显示没有导航栏来保存/关闭弹出窗口
      【解决方案4】:

      给对此有问题的人的说明:

      这个

      - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController:(UIPresentationController *) controller traitCollection:(UITraitCollection *)traitCollection {
          return UIModalPresentationNone;
      }
      

      和这个不一样

      - (UIModalPresentationStyle)adaptivePresentationStyleForPresentationController: (UIPresentationController * ) controller {
          return UIModalPresentationNone;
      }
      

      后者不会被调用/与前者一样工作。

      【讨论】:

        猜你喜欢
        • 2015-01-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-11-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-09-04
        相关资源
        最近更新 更多