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

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

问题描述

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





这就是它在iPhone 6 Plus领域的外观(它几乎拉伸从上到下):



当它显示为这样时,单击视图外部不会忽略它(虽然取消确实有效)。旋转回肖像使其恢复正常。



UIViewController 中的所有约束都安装在所有尺寸上类。



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




  • po self.view:frame =(0 0; 250 394)

  • po self.preferredContentSize(width = 250,height = 160 )



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



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



编辑以包含我的popover代码:



此代码位于 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的宽度)出现在屏幕中间。



<因此,优选的内容大小没有效果。这不是一个弹出窗口。呈现的视图控制器视图具有标准大小,并且这个视图也不例外。



但是,可以显示弹出框作为iPhone上的popover 。为此:




  • 设置popover视图控制器的 popoverPresentationController的委托 呈现之前。


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




然而,这显然是在横向模式下无法使用iPhone 6 Plus; popover不是适应。我会将此描述为一个错误!



编辑在iOS 9中,问题解决了!实施新的委托方法 adaptivePresentationStyleForPresentationController:traitCollection:返回 .None 并且在任何情况下都会得到一个弹出框,包括iPhone 6加上风景。这是一个完整的工作示例,其中创建弹出窗口并在代码中召唤以响应按钮点击:

  @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 =(发件人为!UIView)
pop.sourceRect =(发件人为!UIView).bounds
}
}
func adaptivePresentationStyleForPresentationController(
controller:UIPresentationController,
traitCollection:UITraitCollection)
- > UIModalPresentationStyle {
return .None
}


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

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.

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

When debugging values in viewDidAppear: I see the following:

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

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

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.)

Edit to include my popover code:

This code is found in prepareForSegue:

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

And then the delegate method:

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

And here is the segue definition in Xcode:

解决方案

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.

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

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

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

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 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天全站免登陆