【问题标题】:Why isn't preferredContentSize used by iPhone 6 Plus Landscape?iPhone 6 Plus Landscape 为什么不使用preferredContentSize?
【发布时间】:2015-07-07 17:11:04
【问题描述】:

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

这是它在 iPhone 6 Plus 横向上的外观(几乎从上到下延伸):

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

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

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

  • po self.view: frame = (0 0; 250 394)
  • po self.preferredContentSize(宽度 = 250,高度 = 160)

是什么导致视图的高度跳到 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 定义:

【问题讨论】:

    标签: ios uiviewcontroller iphone-6-plus


    【解决方案1】:

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

    因此,首选内容大小无效。这不是弹出窗口。呈现的视图控制器视图具有标准尺寸,这个也不例外。

    但是,您可以让弹出框在 iPhone 上显示为弹出框。这样做:

    • 为弹出视图控制器的popoverPresentationController设置一个委托呈现它之前。

    • 在委托中,实现adaptivePresentationStyleForPresentationController:并返回.None

    但是,这显然不适用于横向模式下的 iPhone 6 Plus;弹出窗口没有“适应”。我会将此描述为一个错误!

    编辑在 iOS 9 中,问题解决了! 实现新的委托方法 adaptivePresentationStyleForPresentationController:traitCollection: 以返回 .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
    }
    

    【讨论】:

    • 鉴于我添加的编辑,听起来我已经在按照您的建议进行操作了。
    • 那个委托方法被调用了吗?
    • 是的。我刚刚在调试中确认了它。
    • 我做了很多测试,我认为你发现了一个错误。如果您太忙而无法向 Apple 报告,请告诉我;如果你愿意,我会做的。
    • 仔细阅读我所说的。 仔细查看我的代码。请注意,我正在谈论的解决问题的方法adaptivePresentationStyleForPresentationController:traitCollection: 与您的代码显示您之前实现的旧adaptivePresentationStyleForPresentationController:不同。它的行为完全不同。它解决了问题,我的屏幕截图证明了这一点。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多