美文网首页
iOS UITableViewCell自身约束冲突

iOS UITableViewCell自身约束冲突

作者: Amuxiaomu | 来源:发表于2022-08-08 11:21 被阅读0次

遇到的问题:
1.translatesAutoresizingMaskIntoConstraints问题

[LayoutConstraints] Changing the translatesAutoresizingMaskIntoConstraints property of the contentView of a UITableViewCell is not supported and will result in undefined behavior, as this property is managed by the owning UITableViewCell. Cell: <****TableViewCell: 0x156408400; baseClass = UITableViewCell; frame = (0 0; 320 44); layer = <CALayer: 0x283862420>>

这个问题是cell的contentView不能设置translatesAutoresizingMaskIntoConstraints这个属性,不能像下面这么使用

    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_greaterThanOrEqualTo(60);
    }];

应为Masonry内部会给View设置这个属性为NO;

2.cell约束冲突问题

[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. 
(
    "<MASLayoutConstraint:0x282bcb8a0 TestView:0x10466c7b0.top == UITableViewCellContentView:0x10466c2f0.top>",
    "<MASLayoutConstraint:0x282bcb900 TestView:0x10466c7b0.bottom == UITableViewCellContentView:0x10466c2f0.bottom>",
    "<MASLayoutConstraint:0x282bcbba0 TestView:0x10466c7b0.height >= 55>",
    "<NSLayoutConstraint:0x282ca9e00 UITableViewCellContentView:0x10466c2f0.height == 44>"
)

Will attempt to recover by breaking constraint 
<MASLayoutConstraint:0x282bcbba0 TestView:0x10466c7b0.height >= 55>

Make a symbolic breakpoint at UIViewAlertForUnsatisfiableConstraints to catch this in the debugger.
The methods in the UIConstraintBasedLayoutDebugging category on UIView listed in <UIKitCore/UIView.h> may also be helpful.

总结:
1.cell的初始化方法不能调用 - (void)layoutIfNeeded; 这个时候调用会导致与cell自身高度冲突
调用这个方法的原因.由于我需要获取子元素布局后的大小只能调用这个方法 并且可以在layoutSubviews这个方法里面获取到子元素的大小
调用setNeedsLayout方法不会出现冲突,但是也进入不了layoutSubviews这个方法.
2.cell内部的约束布局(可以理解默认布局)不能放在updateConstraints这个方法中(这个方法书写方式是参考Masonry中的),这个方式会导致约束没有撑开,没有效果

相关文章

网友评论

      本文标题:iOS UITableViewCell自身约束冲突

      本文链接:https://www.haomeiwen.com/subject/qjlwwrtx.html