UIAlertController与UICollectionViewFlowLayout

时间:2016-01-01 20:09:42

标签: swift uicollectionview uicollectionviewlayout

嘿,新年快乐!

enter image description here

我有一个奇怪的问题,我的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上的按钮没有出现,但请看图像 enter image description here

有什么建议吗?

基于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)
  }
}

1 个答案:

答案 0 :(得分:2)

简短回答:请勿使用扩展程序。

Swift中的扩展程序是全局的,因此您对UICollectionViewLayout的扩展程序正在影响所有 UICollectionViewLayout类。

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

如果你真的需要它,你应该使用一个子类,但没有理由(我知道)你无法初始化UICollectionViewFlowLayout,将它分配给var并使用它。 / p>