约束导致调试错误

时间:2018-09-16 19:50:57

标签: ios swift constraints uicollectionviewcell

晚上,

我建立了带有动态单元格的UICollectionView,因此高度可以根据内部内容进行调整。集合视图由左侧的个人资料图片,用户名,商品标题和旁边的选定注释组成。

我通过遵循教程,阅读文章并在Stack Overflow上来回往返来完成所有这些工作。但是,在调试日志中,我看到了一些关于约束的抱怨,而且我似乎无法弄清为什么它们首先出现。

我根据所有固定或可变值设置单元格的高度:用户名标签的高度(固定),标题标签的高度(可变)和注释的高度(也可变)。我在collectionView sizeForItemAt方法中执行此操作,如下所示:

    let dummyTitle = "The title of my item, which can also be very long so I am wondering what will happen now"
    let dummyText = "This is a long text which should automatically adapt inside the cell and then the cell height should also adapt so everything is nice and visible."
    let rectWidth = view.frame.width - 32 - 60 - 16
    let rect = NSString(string: dummyText).boundingRect(with:CGSize(width: rectWidth, height: 1000), options: NSStringDrawingOptions.usesFontLeading.union(NSStringDrawingOptions.usesLineFragmentOrigin), attributes: [NSAttributedStringKey.font: UIFont.systemFont(ofSize: 16)], context: nil)
    let rectTitle = NSString(string: dummyTitle).boundingRect(with:CGSize(width: rectWidth, height: 1000), options: NSStringDrawingOptions.usesFontLeading.union(NSStringDrawingOptions.usesLineFragmentOrigin), attributes: [NSAttributedStringKey.font: UIFont.boldSystemFont(ofSize: 16)], context: nil)
    let variableHeight = 16 + 20 + 2 + 2 + 16 + 1 + rect.height + rectTitle.height
    return CGSize(width: view.frame.width, height: variableHeight)

基本上,我通过重新创建一个矩形来计算两个变量(标题标签和注释)的高度,然后获取它们的高度。为了使单元格的高度可变,我在顶部,左侧和右侧设置了约束,以便底部可以是动态的:

    // horizontal constraints profile pic & username
    addConstraintsWithFormat(format: "H:|-16-[v0(60)]-16-[v1]-16-|", views: profilePictureImageView, usernameLabel)
    // horizontal constraints title
    addConstraintsWithFormat(format: "H:|-92-[v0]-16-|", views: titleLabel)
    // horizontal constraints comment
    addConstraintsWithFormat(format: "H:|-92-[v0]-16-|", views: commentTextView)
    // vertical constraints profile pic & separator
    addConstraintsWithFormat(format: "V:|-16-[v0(60)]-16-[v1(1)]|", views: profilePictureImageView, separatorView)
    // horizontal constraints separator
    addConstraintsWithFormat(format: "H:|[v0]|", views: separatorView)
    // vertical constraints username
    addConstraintsWithFormat(format: "V:|-16-[v0(20)]-2-|", views: usernameLabel)
    // top constraints workout
    addConstraints([NSLayoutConstraint(item: titleLabel, attribute: .top, relatedBy: .equal, toItem: usernameLabel, attribute: .bottom, multiplier: 1, constant: 2)])
    // top constraint comment
    addConstraints([NSLayoutConstraint(item: commentTextView, attribute: .top, relatedBy: .equal, toItem: titleLabel, attribute: .bottom, multiplier: 1, constant: 2)])

该应用程序可以正常运行并显示相应的所有内容,但是我想进行一个干净的调试,因为恐怕以后可能会引起问题。我还想了解为什么首先会发生此错误,所以我可以在以后的项目中避免它。

编辑:添加了调试日志的一部分。这是13个错误中的两个:

2018-09-16 16:20:30.030871+0200 prjExplanation[12946:529268] [LayoutConstraints] 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. 
(
    "<NSLayoutConstraint:0x6040002810e0 V:|-(16)-[UIImageView:0x7fe8b2f1dd00]   (active, names: '|':prjExplanation.socialCell:0x7fe8b2c1c970 )>",
    "<NSLayoutConstraint:0x604000281360 UIImageView:0x7fe8b2f1dd00.height == 60   (active)>",
    "<NSLayoutConstraint:0x6040002816d0 V:[UIImageView:0x7fe8b2f1dd00]-(16)-[UIView:0x7fe8b2c21a70]   (active)>",
    "<NSLayoutConstraint:0x604000281680 UIView:0x7fe8b2c21a70.height == 1   (active)>",
    "<NSLayoutConstraint:0x6040002811d0 V:[UIView:0x7fe8b2c21a70]-(0)-|   (active, names: '|':prjExplanation.socialCell:0x7fe8b2c1c970 )>",
    "<NSLayoutConstraint:0x604000281310 V:|-(16)-[UILabel:0x7fe8b2f1e4f0'Author_203']   (active, names: '|':prjExplanation.socialCell:0x7fe8b2c1c970 )>",
    "<NSLayoutConstraint:0x60400009f8b0 UILabel:0x7fe8b2f1e4f0'Author_203'.height == 20   (active)>",
    "<NSLayoutConstraint:0x60400009c430 V:[UILabel:0x7fe8b2f1e4f0'Author_203']-(2)-|   (active, names: '|':prjExplanation.socialCell:0x7fe8b2c1c970 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x6040002816d0 V:[UIImageView:0x7fe8b2f1dd00]-(16)-[UIView:0x7fe8b2c21a70]   (active)>

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.
2018-09-16 16:20:30.043291+0200 prjExplanation[12946:529268] [LayoutConstraints] 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. 
(
    "<NSLayoutConstraint:0x6040002820d0 V:|-(16)-[UIImageView:0x7fe8b2f274a0]   (active, names: '|':prjExplanation.socialCell:0x7fe8b2f26bc0 )>",
    "<NSLayoutConstraint:0x604000281ea0 UIImageView:0x7fe8b2f274a0.height == 60   (active)>",
    "<NSLayoutConstraint:0x604000282300 V:[UIImageView:0x7fe8b2f274a0]-(16)-[UIView:0x7fe8b2d0b880]   (active)>",
    "<NSLayoutConstraint:0x604000282120 UIView:0x7fe8b2d0b880.height == 1   (active)>",
    "<NSLayoutConstraint:0x604000281d60 V:[UIView:0x7fe8b2d0b880]-(0)-|   (active, names: '|':prjExplanation.socialCell:0x7fe8b2f26bc0 )>",
    "<NSLayoutConstraint:0x6040002823f0 V:|-(16)-[UILabel:0x7fe8b2f276d0'Author_203']   (active, names: '|':prjExplanation.socialCell:0x7fe8b2f26bc0 )>",
    "<NSLayoutConstraint:0x6040002823a0 UILabel:0x7fe8b2f276d0'Author_203'.height == 20   (active)>",
    "<NSLayoutConstraint:0x604000281ef0 V:[UILabel:0x7fe8b2f276d0'Author_203']-(2)-|   (active, names: '|':prjExplanation.socialCell:0x7fe8b2f26bc0 )>"
)

Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x604000282300 V:[UIImageView:0x7fe8b2f274a0]-(16)-[UIView:0x7fe8b2d0b880]   (active)>

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.

0 个答案:

没有答案