UIAlertController 与 UICollectionViewFlowLayout 冲突

UIAlertController conflicts with UICollectionViewFlowLayout

大家好,新年快乐!

我有一个奇怪的问题,我的 UICollectionViewFlowLayout 扩展与 UIAlertController 冲突,并删除了它的按钮。

我的应用有一个启用了分页的 uicollectionviewcontroller,我的 uicollectionviewflowlayout 扩展确保单个单元格填满整个屏幕

// Extension to make a UICollectionViewCell fill the whole screen
extension UICollectionViewFlowLayout {
  public override func prepareLayout() {
    let ratio : CGFloat = 5.6 / 8.7

    let cardHeight = collectionView!.frame.height * 0.7
    let cardWidth = cardHeight * ratio
    let spacing = (collectionView!.frame.height - cardHeight) / 2

    itemSize = CGSize(width: cardWidth, height: cardHeight)
    minimumLineSpacing = spacing*2
    sectionInset = UIEdgeInsets(top: spacing, left: 0, bottom: spacing, right: 0)
  }
}

每当我调用 UIAlertController

let alertController = UIAlertController(title: "Error", message: "Error", preferredStyle: .Alert)
alertController.addAction(UIAlertAction(title: "OK", style: .Cancel) { (action) in })
self.presentViewController(alertController, animated: true) {}

我在控制台中收到以下消息

2016-01-01 21:04:48.924 Jeg Har Aldrig[10912:3649777] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x15f6c4e80 h=--& v=--& H:[_UIAlertControllerActionView:0x15f53d9e0(19.8253)]>",
    "<NSLayoutConstraint:0x15f67b370 H:|-(>=12)-[UIView:0x15f53e0c0]   (Names: '|':_UIAlertControllerActionView:0x15f53d9e0 )>",
    "<NSLayoutConstraint:0x15f67b3c0 UIView:0x15f53e0c0.trailing <= _UIAlertControllerActionView:0x15f53d9e0.trailing - 12>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x15f67b3c0 UIView:0x15f53e0c0.trailing <= _UIAlertControllerActionView:0x15f53d9e0.trailing - 12>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2016-01-01 21:04:48.926 Jeg Har Aldrig[10912:3649777] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x15f6c62a0 h=--& v=--& V:[_UIAlertControllerActionView:0x15f53d9e0(30.8)]>",
    "<NSLayoutConstraint:0x15f67bd50 V:[_UIAlertControllerActionView:0x15f53d9e0(>=44)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x15f67bd50 V:[_UIAlertControllerActionView:0x15f53d9e0(>=44)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2016-01-01 21:04:48.927 Jeg Har Aldrig[10912:3649777] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x15f6c4e80 h=--& v=--& H:[_UIAlertControllerActionView:0x15f53d9e0(19.8253)]>",
    "<NSLayoutConstraint:0x15f67b370 H:|-(>=12)-[UIView:0x15f53e0c0]   (Names: '|':_UIAlertControllerActionView:0x15f53d9e0 )>",
    "<NSLayoutConstraint:0x15f67b3c0 UIView:0x15f53e0c0.trailing <= _UIAlertControllerActionView:0x15f53d9e0.trailing - 12>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x15f67b3c0 UIView:0x15f53e0c0.trailing <= _UIAlertControllerActionView:0x15f53d9e0.trailing - 12>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2016-01-01 21:04:48.927 Jeg Har Aldrig[10912:3649777] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x15f6c62a0 h=--& v=--& V:[_UIAlertControllerActionView:0x15f53d9e0(30.8)]>",
    "<NSLayoutConstraint:0x15f67bd50 V:[_UIAlertControllerActionView:0x15f53d9e0(>=44)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x15f67bd50 V:[_UIAlertControllerActionView:0x15f53d9e0(>=44)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2016-01-01 21:04:48.938 Jeg Har Aldrig[10912:3649777] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x15f6c4e80 h=--& v=--& H:[_UIAlertControllerActionView:0x15f53d9e0(19.8253)]>",
    "<NSLayoutConstraint:0x15f67b370 H:|-(>=12)-[UIView:0x15f53e0c0]   (Names: '|':_UIAlertControllerActionView:0x15f53d9e0 )>",
    "<NSLayoutConstraint:0x15f67b3c0 UIView:0x15f53e0c0.trailing <= _UIAlertControllerActionView:0x15f53d9e0.trailing - 12>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x15f67b3c0 UIView:0x15f53e0c0.trailing <= _UIAlertControllerActionView:0x15f53d9e0.trailing - 12>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.
2016-01-01 21:04:48.940 Jeg Har Aldrig[10912:3649777] Unable to simultaneously satisfy constraints.
    Probably at least one of the constraints in the following list is one you don't want. 
    Try this: 
        (1) look at each constraint and try to figure out which you don't expect; 
        (2) find the code that added the unwanted constraint or constraints and fix it. 
    (Note: If you're seeing NSAutoresizingMaskLayoutConstraints that you don't understand, refer to the documentation for the UIView property translatesAutoresizingMaskIntoConstraints) 
(
    "<NSAutoresizingMaskLayoutConstraint:0x15f6c62a0 h=--& v=--& V:[_UIAlertControllerActionView:0x15f53d9e0(30.8)]>",
    "<NSLayoutConstraint:0x15f67bd50 V:[_UIAlertControllerActionView:0x15f53d9e0(>=44)]>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x15f67bd50 V:[_UIAlertControllerActionView:0x15f53d9e0(>=44)]>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKit/UIView.h> may also be helpful.

虽然我的 UIAlertControllers 上的按钮没有出现,请看图片

有什么建议吗?

基于 Mattvens 答案的解决方案

我没有做扩展,而是做了一个子类:

class FullCellLayout: UICollectionViewFlowLayout {
  required init?(coder aDecoder: NSCoder) {
    super.init(coder: aDecoder)

    let ratio : CGFloat = 5.6 / 8.7

    let cardHeight = UIScreen.mainScreen().bounds.height * 0.7
    let cardWidth = cardHeight * ratio
    let spacing = (UIScreen.mainScreen().bounds.height - cardHeight) / 2

    itemSize = CGSize(width: cardWidth, height: cardHeight)
    minimumLineSpacing = spacing*2
    sectionInset = UIEdgeInsets(top: spacing, left: 0, bottom: spacing, right: 0)
  }
}

简答:不要为此使用扩展程序。

Swift 中的扩展是全局的,因此您对 UICollectionViewLayout 的扩展影响 all UICollectionViewLayout 类.

根据您收到的错误判断,UIAlertController 在内部使用 UICollectionView 布局按钮,而您的扩展程序破坏了该功能。

你应该使用一个子类,如果你真的需要它,但没有理由(据我所知)你不能初始化 UICollectionViewFlowLayout,将它分配给 var 并使用它.