为什么 iPhone 6 Plus Landscape 不使用 preferredContentSize? [英] Why isn't preferredContentSize used by iPhone 6 Plus Landscape?

查看:14
本文介绍了为什么 iPhone 6 Plus Landscape 不使用 preferredContentSize?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In my iOS 8 app, this popover segue appears correctly on all devices in all orientations except for iPhone 6 Plus landscape:

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

This is how it looks on iPhone 6 Plus landscape (it is stretching almost from top to bottom):

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

And when it displays like this, clicking outside of the view doesn't dismiss it (although Cancel does work). Rotating back to portrait gets it back to normal.

这个 UIViewController 中的所有约束都安装在所有尺寸类上.

All of the constraints in this UIViewController are installed on all size classes.

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

When debugging values in viewDidAppear: I see the following:

  • po self.view:frame = (0 0; 250 394)
  • po self.preferredContentSize (width = 250, height = 160)

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

What is causing the view's height to jump to 394?

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

I'm actually having the same issue with another popover segue in iPhone 6 Plus landscape as well. (And in case there was curiosity, I'm using a VC instead of 'UIAlertController' here because of the validation requirements of the UITextField displayed don't work well with 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 定义:

And here is the segue definition in Xcode:

推荐答案

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

What you're seeing is not a popover. It's a normal presented view. By default, a popover appears as a popover on iPad, but as a presented view on iPhone — including the iPhone 6 plus. On other iPhones, this presented view is fullscreen - it covers everything. But the iPhone 6 is so wide that they don't do that, so it appears in the middle of the screen at a standard width (the width of a smaller iPhone).

因此,首选内容大小没有影响.这不是弹窗.呈现的视图控制器视图具有标准大小,这个也不例外.

Thus, the preferred content size has no effect. This isn't a popover. Presented view controller views are given a standard size, and this one is no exception.

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

However, you can have the popover appear as a popover on iPhone. To do so:

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

  • Set a delegate for the popover view controller's popoverPresentationController before presenting it.

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

In the delegate, implement adaptivePresentationStyleForPresentationController: and return .None.

然而,这显然不适用于横屏模式的 iPhone 6 Plus;弹出窗口不是适应".我会把这描述为一个错误!

However, this is apparently not working on iPhone 6 Plus in landscape mode; the popover is not "adapting". I would describe this as a bug!

EDIT 在 iOS 9 中,问题解决了! 实现新的委托方法 adaptivePresentationStyleForPresentationController:traitCollection: 返回 .None 并且您在所有情况下都会看到一个弹出框,包括 iPhone 6 Plus 的横屏.这是一个完整的工作示例,其中弹出窗口是在代码中创建和调用以响应按钮点击:

EDIT In iOS 9, the problem is solved! Implement the new delegate method adaptivePresentationStyleForPresentationController:traitCollection: to return .None and you'll get a popover under all circumstances, including the iPhone 6 Plus in landscape. Here's a complete working example where the popover is created and summoned in code in response to a button tap:

@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
}

这篇关于为什么 iPhone 6 Plus Landscape 不使用 preferredContentSize?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆