UICollectionView自动布局约束错误

时间:2015-07-25 07:53:10

标签: ios objective-c uicollectionview uicollectionviewcell uicollectionviewlayout

我有一个带有一个原型单元的简单UICollectionVewController。我所拥有的只是一个UILabel。

我选择了尺寸等级和自动布局。我为

设置了约束
top (Top Space to cell): = 100
Height >= 17
Trailing space to Cell = 0
Leading Space to Cell = 0

在此配置中,它可以正常工作。但是我想在单元格上有左右边距,所以我将前导和尾随空格更改为10。

虽然这有效,但我收到以下错误:

  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) 
        (
        "<NSLayoutConstraint:0x1740907c0 H:[UILabel:0x14de1a380'This is a test to see wha...']-(10)-|   (Names: '|':UIView:0x14de1a270 )>",
        "<NSLayoutConstraint:0x174090810 H:|-(10)-[UILabel:0x14de1a380'This is a test to see wha...']   (Names: '|':UIView:0x14de1a270 )>",
        "<NSAutoresizingMaskLayoutConstraint:0x174090d10 h=--& v=--& H:[UIView:0x14de1a270(0)]>"
        )

        Will attempt to recover by breaking constraint 
        <NSLayoutConstraint:0x1740907c0 H:[UILabel:0x14de1a380'This is a test to see wha...']-(10)-|   (Names: '|':UIView:0x14de1a270 )>

        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.

如果我将前导空格设置为10并删除训练空间并设置宽度约束,我就不会收到错误。

但是,单元格的宽度是可变的,因此我无法设置宽度。

我可以解决它,通过创建宽度约束的出口并执行此操作:

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
MyCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];


CGRect colViewSize = collectionView.bounds;

int width = colViewSize.size.width;
width -= 28; // 10px either size and 8px in middle
width = width/2;
width -=14; // margin of 7 (x 2)

cell.labelconstraintWidth.constant = width;
return cell;
然而,这似乎是一个黑客攻击。

有没有人知道这里发生了什么。

2 个答案:

答案 0 :(得分:1)

简单地说,您的视图会调整大小,当它的大小调整时,由于视图的宽度小于20,因此无法满足10个前导空格和10个尾随空格的约束。

解决方案?如果您不想考虑它,可以将百分比标签边距/宽度设置为超级视图或使边距为零。

但是,如果您有一个计算单元格宽度的封装逻辑,则可以轻松地从代码中添加边距约束。

修改

您可能有一个50%的自动布局约束,但这不是问题。你有一个影响你视图宽度的AutoResizingMaskLayoutConstraint。

要在故事板中创建百分比边距,请执行以下操作:

1)在视图层次结构中选择您的视图

2)按住CTRL并拖动到你的超级视图(或超视图的完整范围的根视图)

3)选择Horizo​​ntal Spacing

4)选择视图并双击水平间距约束

5)在尺寸检查器中选择:

第一项:YourView.Leading

关系:平等

第二项:YourSuperview.Trailing

常数:0

优先级:1000

乘数:(您的保证金百分比)

正如@Bamsworld所说,你应该仔细检查AutoResizing面具。它们可能与您的AutoLayout约束冲突。

答案 1 :(得分:0)

错误告诉您不需要一个或多个约束,并将其中一个约束列为 NSAutoresizingMaskLayoutConstraint 。似乎与自动布局 autoresizingmasks 存在冲突。我建议使用其中一种。

尝试通过在视图上调用 setTranslatesAutoresizingMaskIntoConstraints 来删除autoresize-constraint,并传入 NO 。同时将视图的 autoresizingMask 属性设置为 UIViewAutoresizingNone (只是为了确定)。