【问题标题】:Autolayoutconstraint warning for fixed height subview固定高度子视图的自动布局约束警告
【发布时间】:2014-06-25 16:11:41
【问题描述】:

我正在使用UIView+Autolayout 来简化基于代码的自动布局约束创建,但我在向 UITableViewCell 子类添加约束时遇到了麻烦。

我在单元格中有一个子视图(“viewUser”),用于对有关发布单元格中显示的其他内容的联系人信息进行分组。 "viewUser" 是固定高度,始终位于单元格底部:

为了在 init 方法中实现这一点,我创建了“viewUser”并将其添加到单元格的 contentView:

self.viewUser = [UIView newAutoLayoutView];

[self.contentView addSubview:self.viewUser];

并在 updateConstraints 方法中添加以下约束:

[self.viewUser autoSetDimension:ALDimensionHeight toSize:kContentHeight];
[self.viewUser autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:0.0f];
[self.viewUser autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:0.0f];
[self.viewUser autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:0.0f];
[self.viewUser autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:0.0f];

当我创建单元格时,我在控制台中收到以下警告:

    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:0xb2a6b40 h=--& v=--& V:[UITableViewCellContentView:0xb289e20(44)]>",
    "<NSLayoutConstraint:0xb2a3360 V:[UIView:0xb2a5fd0(53)]>",
    "<NSLayoutConstraint:0xb2a3420 V:|-(0)-[UIView:0xb2a5fd0]   (Names: '|':UITableViewCellContentView:0xb289e20 )>",
    "<NSLayoutConstraint:0xb2a3840 UIView:0xb2a5fd0.bottom == UITableViewCellContentView:0xb289e20.bottom>" )

Will attempt to recover by breaking constraint  <NSLayoutConstraint:0xb2a3360 V:[UIView:0xb2a5fd0(53)]>

这对我来说是有道理的,因为我说“viewUser”应该有一个固定的高度,然后我将它固定到 contentView 的边缘但是如果我删除:

[self.viewUser autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:0.0f];

当我在表格视图控制器中计算单元格的动态高度时,它的高度返回为 0。

我无法弄清楚我做错了什么以及如何删除此警告并获取单元格的高度,任何见解都会很棒。

【问题讨论】:

    标签: ios uitableview nslayoutconstraint autolayout


    【解决方案1】:

    如果不了解单元格的整个布局,就很难诊断出问题所在。很可能,您在

    中返回了不正确的高度
    - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
    

    您返回的高度对于“viewUser”和上面的内容子视图都太小了。

    如果您的“viewUser”正在消失,我现在可以想到两种情况,这可能是由于您的表格单元格高度太小。:

    1. 您对 viewUser 的高度限制在 由于约束冲突而导致的运行时。这就是发生在 您发布的警告:对于您的 viewUser,单元格高度太小 (44) 高度 (53)

    2. 您的 viewUser 的身高被更高的优先级取代 可能来自内容视图的约束。这不太可能,但也是一种可能性。

    【讨论】:

    • 这里的问题绝对是#1。要解决这个问题,请做两件事: 1. 开始时让单元格非常大,例如cell.bounds = CGRectMake(0, 0, 9999, 9999),这样你就有足够的“空间”来使用。然后, 2. 将单元格顶部/底部边缘的至少 1 个垂直约束更改为不等式(具体而言,大于或等于)。例如[self.viewUser autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:0.0f relation:NSLayoutRelationGreaterThanOrEqual];。如果单元格比要求的高,这可以防止异常(当然在合适的尺寸下也可以正常工作)。
    猜你喜欢
    • 1970-01-01
    • 2017-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-25
    • 2013-12-04
    • 2014-10-21
    • 1970-01-01
    相关资源
    最近更新 更多