为什么 iPhone 6 Plus Landscape 不使用 preferredContentSize?

Why isn't preferredContentSize used by iPhone 6 Plus Landscape?

在我的 iOS 8 应用程序中,此弹出窗口转场在所有方向的所有设备上正确显示,除了 iPhone 6 Plus 横向:

这是在 iPhone 6 Plus 横向上的样子(它几乎从上到下伸展):

当它像这样显示时,在视图外部单击不会关闭它(尽管取消确实有效)。旋转回纵向使其恢复正常。

UIViewController 中的所有约束都安装在所有大小 类 上。

调试 viewDidAppear: 中的值时,我看到以下内容:

是什么导致视图的高度跳到 394?

我实际上在 iPhone 6 Plus 景观中也遇到了另一个 popover segue 的相同问题。 (如果有好奇心,我在这里使用 VC 而不是 'UIAlertController',因为显示的 UITextField 的验证要求不适用于 UIAlertController .)

编辑以包含我的弹出代码:

此代码位于 prepareForSegue:

    FavoriteNameViewController *nameVC = segue.destinationViewController;
    UIPopoverPresentationController *popPC = nameVC.popoverPresentationController;
    popPC.delegate = self;
    nameVC.delegate = self;
    nameVC.view.center = self.originalContentView.center;

然后是委托方法:

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

这里是 Xcode 中的 segue 定义:

您看到的不是弹出窗口。这是一个正常的呈现视图。默认情况下,弹出窗口在 iPad 上显示为弹出窗口,但在 iPhone 上显示为呈现视图 — 包括 iPhone 6 plus。在其他 iPhone 上,此呈现的视图是全屏的 - 它涵盖了所有内容。但是 iPhone 6 太宽了,他们不这样做,所以它以标准宽度(较小的 iPhone 的宽度)出现在屏幕中间。

因此,首选内容大小没有影响。这不是弹出窗口。 Presented view controller views 被赋予一个标准尺寸,这个也不例外。

但是,您可以让弹出窗口显示为iPhone上的弹出窗口。为此:

  • 在显示弹出视图控制器popoverPresentationController之前设置一个委托。

  • 在委托中,实现adaptivePresentationStyleForPresentationController:和return.None

然而,这显然不适用于 iPhone 6 Plus 的横向模式;弹出窗口不是 "adapting"。我会将其描述为错误!

编辑在iOS9中,问题解决!实现新的委托方法adaptivePresentationStyleForPresentationController:traitCollection:到return .None 并且在任何情况下你都会得到一个弹出窗口,包括 iPhone 6 Plus 的横屏。这是一个完整的工作示例,其中在代码中创建和调用弹出窗口以响应按钮点击:

@IBAction func doButton(sender: AnyObject) {
    let vc = MyViewController()
    vc.preferredContentSize = CGSizeMake(400,500)
    vc.modalPresentationStyle = .Popover
    if let pres = vc.presentationController {
        pres.delegate = self
    }
    self.presentViewController(vc, animated: true, completion: nil)
    if let pop = vc.popoverPresentationController {
        pop.sourceView = (sender as! UIView)
        pop.sourceRect = (sender as! UIView).bounds
    }
}
func adaptivePresentationStyleForPresentationController(
    controller: UIPresentationController, 
    traitCollection: UITraitCollection) 
    -> UIModalPresentationStyle {
        return .None
}