Swift 使用动态标题大小UICollectionView时,UIViewAlertForUnsatifiableConstraints错误 简介

Swift 使用动态标题大小UICollectionView时,UIViewAlertForUnsatifiableConstraints错误 简介,swift,uicollectionview,autolayout,uikit,uicollectionviewflowlayout,Swift,Uicollectionview,Autolayout,Uikit,Uicollectionviewflowlayout,我正在尝试为collectionView的标题单元格设置动态单元格大小。用作标题的单元格在另一个collectionView中也用作“常规”单元格。在那里,使用动态单元格大小调整工作正常 这就是collectionView的创建方式: private lazy var timelineCollectionView: UICollectionView = { let flowLayout = UICollectionViewFlowLayout() flowLayout.scrol

我正在尝试为collectionView的标题单元格设置动态单元格大小。用作标题的单元格在另一个collectionView中也用作“常规”单元格。在那里,使用动态单元格大小调整工作正常

这就是collectionView的创建方式:

private lazy var timelineCollectionView: UICollectionView = {
    let flowLayout = UICollectionViewFlowLayout()
    flowLayout.scrollDirection = .vertical
    flowLayout.estimatedItemSize = CGSize(width: self.view.frame.width, height: 10)
    flowLayout.minimumInteritemSpacing = 0
    flowLayout.minimumLineSpacing = 0
    
    let cv = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
    cv.alwaysBounceVertical = true
    cv.contentInsetAdjustmentBehavior = .never
    cv.backgroundColor = .clear
    cv.scrollIndicatorInsets = UIEdgeInsets(top: self.coloredTitleBarHeight - self.getStatusBarHeight(), left: 0, bottom: 0, right: 0)
    
    cv.delegate = self
    cv.dataSource = self
    cv.register(TLContentCell.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "content-header-cell")
    
    return cv
}()
private lazy var width: NSLayoutConstraint = {
        let width = contentView.widthAnchor.constraint(equalToConstant: bounds.size.width)
        width.isActive = true
        return width
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.translatesAutoresizingMaskIntoConstraints = false
        contentView.backgroundColor = UIColor.white
    }
    
    override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
        width.constant = bounds.size.width
        return contentView.systemLayoutSizeFitting(CGSize(width: targetSize.width, height: 1))
    }

func setupViews() {
     // view setup happens here
     if let lastSubview = contentView.subviews.last {
         contentView.bottomAnchor.constraint(equalTo: lastSubview.bottomAnchor, constant: 0).isActive = true
     }
}
我一直遵循动态单元格高度

在本教程之后,这是my cell中的一段代码:

private lazy var timelineCollectionView: UICollectionView = {
    let flowLayout = UICollectionViewFlowLayout()
    flowLayout.scrollDirection = .vertical
    flowLayout.estimatedItemSize = CGSize(width: self.view.frame.width, height: 10)
    flowLayout.minimumInteritemSpacing = 0
    flowLayout.minimumLineSpacing = 0
    
    let cv = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
    cv.alwaysBounceVertical = true
    cv.contentInsetAdjustmentBehavior = .never
    cv.backgroundColor = .clear
    cv.scrollIndicatorInsets = UIEdgeInsets(top: self.coloredTitleBarHeight - self.getStatusBarHeight(), left: 0, bottom: 0, right: 0)
    
    cv.delegate = self
    cv.dataSource = self
    cv.register(TLContentCell.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "content-header-cell")
    
    return cv
}()
private lazy var width: NSLayoutConstraint = {
        let width = contentView.widthAnchor.constraint(equalToConstant: bounds.size.width)
        width.isActive = true
        return width
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.translatesAutoresizingMaskIntoConstraints = false
        contentView.backgroundColor = UIColor.white
    }
    
    override func systemLayoutSizeFitting(_ targetSize: CGSize, withHorizontalFittingPriority horizontalFittingPriority: UILayoutPriority, verticalFittingPriority: UILayoutPriority) -> CGSize {
        width.constant = bounds.size.width
        return contentView.systemLayoutSizeFitting(CGSize(width: targetSize.width, height: 1))
    }

func setupViews() {
     // view setup happens here
     if let lastSubview = contentView.subviews.last {
         contentView.bottomAnchor.constraint(equalTo: lastSubview.bottomAnchor, constant: 0).isActive = true
     }
}
同样,如果我将该单元格用作常规单元格,而不是在collectionView的页眉或页脚中,则该代码完全可以正常工作。

现在,我只想在标题中使用它。由于无法为标题单元格提供类似于
estimatedItemSize
的内容,因此我使用以下方法,可在中找到:

实际上,标题单元格的大小调整正确。但是,UIKit打印的
UIViewAlertforUnsatifiableConstraints
错误似乎无穷无尽(由于SO字符限制而缩短):

2020-01-16 15:29:00.427001+0100 MyApp[1542:65269][LayoutConstraints]无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。
试试这个:
(1) 看看每一个约束,试着找出你不期望的;
(2) 找到添加了不需要的约束的代码,然后修复它。
(
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""
)
将尝试通过打破约束进行恢复
在UIViewAlertForUnsatifiableConstraints处创建一个符号断点,以便在调试器中捕获该断点。
中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。
2020-01-16 15:29:00.427892+0100 MyApp[1542:65269][LayoutConstraints]无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。
试试这个:
(1) 看看每一个约束,试着找出你不期望的;
(2) 找到添加了不需要的约束的代码,然后修复它。
(
"",
"",
"",
"",
"",
"",
"",
"",
""
)
将尝试通过打破约束进行恢复
在UIViewAlertForUnsatifiableConstraints处创建一个符号断点,以便在调试器中捕获该断点。
中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。
2020-01-16 15:29:00.428596+0100 MyApp[1542:65269][LayoutConstraints]无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。
试试这个:
(1) 看看每一个约束,试着找出你不期望的;
(2) 找到添加了不需要的约束的代码,然后修复它。
(
"",
"",
"",
"",
"",
"",
"",
"",
"",
""
)
将尝试通过打破约束进行恢复
在UIViewAlertForUnsatifiableConstraints处创建一个符号断点,以便在调试器中捕获该断点。
中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。
2020-01-16 15:29:00.429543+0100 MyApp[1542:65269][LayoutConstraints]无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。
试试这个:
(1) 看看每一个约束,试着找出你不期望的;
(2) 找到添加了不需要的约束的代码,然后修复它。
(
"",
"",
"",
"",
"",
"",
"",
"",
""
)
将尝试通过打破约束进行恢复
在UIViewAlertForUnsatifiableConstraints处创建一个符号断点,以便在调试器中捕获该断点。
中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。
2020-01-16 15:29:00.430324+0100 MyApp[1542:65269][LayoutConstraints]无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。
试试这个:
(1) 看看每一个约束,试着找出你不期望的;
(2) 找到添加了不需要的约束的代码,然后修复它。
(
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
"",
""
)
将尝试通过打破约束进行恢复
在UIViewAlertForUnsatifiableConstraints处创建一个符号断点,以便在调试器中捕获该断点。
中列出的UIView上UIConstraintBasedLayoutDebugging类别中的方法也可能会有所帮助。
2020-01-16 15:29:00.431297+0100 MyApp[1542:65269][LayoutConstraints]无法同时满足约束。
可能下面列表中至少有一个约束是您不想要的。
试试这个:
(1) 看看每一个约束,试着找出你不期望的;
(2) 找到添加了不需要的约束的代码,然后修复它。
(
"",
"",
"",
"",
"",

“我们可以将这些错误插入,更好地了解发生了什么

让我们从开始。最后3行在这里很有趣:

  • View1的前缘应等于View2的前缘加上16
  • View1的后缘应等于View2的后缘减去16
  • View2的宽度应等于0
但是,似乎中断约束是图像视图的宽度设置为45。
UIImageView:0x7fa6fc578020.width==45

还有另一个约束将imageView和View1配对在一起:
imageView的前缘应等于View1的前缘。

有了这些细节,我们就可以开始形成一幅关于这是如何布置的画面。
(
    "<NSLayoutConstraint:0x600000025680 H:|-(0)-[MyApp.PostInteractionButton:0x7fa6fc5720f0](LTR)   (active, names: '|':UIView:0x7fa6fc579e80 )>",
    "<NSLayoutConstraint:0x600000025720 MyApp.PostInteractionButton:0x7fa6fc5720f0.right == MyApp.DefaultLabel:0x7fa6fc573090'1'.left - 8   (active)>",
    "<NSLayoutConstraint:0x600000025810 MyApp.DefaultLabel:0x7fa6fc573090'1'.right == UIView:0x7fa6fc579e80.right   (active)>",
    "<NSLayoutConstraint:0x600000025ef0 H:|-(16)-[UIStackView:0x7fa6fc579cf0](LTR)   (active, names: '|':UIView:0x7fa6fc56b450 )>",
    "<NSLayoutConstraint:0x600000025540 UIStackView:0x7fa6fc579cf0.right == UIView:0x7fa6fc56b450.right - 16   (active)>",
    "<NSLayoutConstraint:0x600000026fd0 UIView:0x7fa6fc56b450.width == 0   (active)>",
    "<NSLayoutConstraint:0x6000000265d0 'UISV-canvas-connection' UIStackView:0x7fa6fc579cf0.leading == UIView:0x7fa6fc579e80.leading   (active)>",
    "<NSLayoutConstraint:0x600000026620 'UISV-canvas-connection' H:[MyApp.PostInteractionButton:0x7fa6fc5752d0]-(0)-|   (active, names: '|':UIStackView:0x7fa6fc579cf0 )>",
    "<NSLayoutConstraint:0x6000000266c0 'UISV-distributing-edge' H:[UIView:0x7fa6fc579e80]-(0)-[_UIOLAGapGuide:0x600001f8cd00'UISV-distributing']   (active)>",
    "<NSLayoutConstraint:0x600000026710 'UISV-distributing-edge' _UIOLAGapGuide:0x600001f8cd00'UISV-distributing'.trailing == UIView:0x7fa6fc57c3e0.leading   (active)>",
    "<NSLayoutConstraint:0x600000026800 'UISV-distributing-edge' H:[UIView:0x7fa6fc57c3e0]-(0)-[_UIOLAGapGuide:0x600001f8d300'UISV-distributing']   (active)>",
    "<NSLayoutConstraint:0x600000026850 'UISV-distributing-edge' _UIOLAGapGuide:0x600001f8d300'UISV-distributing'.trailing == UIView:0x7fa6fc57c6d0.leading   (active)>",
    "<NSLayoutConstraint:0x6000000267b0 'UISV-distributing-edge' H:[UIView:0x7fa6fc57c6d0]-(0)-[_UIOLAGapGuide:0x600001f8d400'UISV-distributing']   (active)>",
    "<NSLayoutConstraint:0x600000026940 'UISV-distributing-edge' _UIOLAGapGuide:0x600001f8d400'UISV-distributing'.trailing == MyApp.PostInteractionButton:0x7fa6fc5752d0.leading   (active)>",
    "<NSLayoutConstraint:0x6000000268a0 'UISV-fill-equally' _UIOLAGapGuide:0x600001f8d300'UISV-distributing'.width == _UIOLAGapGuide:0x600001f8cd00'UISV-distributing'.width   (active)>",
    "<NSLayoutConstraint:0x600000026990 'UISV-fill-equally' _UIOLAGapGuide:0x600001f8d400'UISV-distributing'.width == _UIOLAGapGuide:0x600001f8cd00'UISV-distributing'.width   (active)>"
)