【问题标题】:UICollectionView auto layout constraint errorUICollectionView 自动布局约束错误
【发布时间】:2015-07-25 07:53:09
【问题描述】:

我有一个带有一个原型单元的简单 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;

然而,这似乎是一个 hack。

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

【问题讨论】:

    标签: ios objective-c uicollectionview uicollectionviewcell uicollectionviewlayout


    【解决方案1】:

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

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

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

    编辑:

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

    为了在情节提要中创建百分比边距,请执行以下操作:

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

    2) 按住 CTRL 并拖动到您的超级视图(或占据超级视图全部边界的根视图)

    3) 选择水平间距

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

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

    第一项:YourView.Leading

    关系:相等

    第二项:YourSuperview.Trailing

    常数:0

    优先级:1000

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

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

    【讨论】:

    • 感谢您的评论。 1) 我的视图宽度设置为屏幕的 50%,因此永远不会设置为小于 20。它是否会先调整大小,然后再调整到所需的大小? - 2) 如何在 Storyboard 中设置百分比标签边距? 3)就您的最后一条评论而言,您是说我在 cellForItem...使用,宽度总是大于20?
    【解决方案2】:

    该错误告诉您不需要一个或多个约束,并将其中一个约束列为 NSAutoresizingMaskLayoutConstraint。似乎与 auto-layout 和 autoresizingmasks 存在冲突。我建议使用其中一个。

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

    【讨论】:

    • 我已尝试将这些属性设置为 NO。我认为 Mercurial 是正确的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-12
    • 1970-01-01
    • 1970-01-01
    • 2013-01-18
    • 2017-02-20
    • 1970-01-01
    相关资源
    最近更新 更多