【问题标题】:Auto layout issue inside table cell that has dynamic height UILabel and UIView(container)具有动态高度 UILabel 和 UIView(容器)的表格单元格内的自动布局问题
【发布时间】:2017-02-11 05:01:47
【问题描述】:

我正在使用具有自动布局的动态高度表格单元格,在表格单元格内部,有 1 个动态高度 UILabel 和 1 个动态 UIView,其中包含多个 UIImageView。

这是布局: Storyboard screenshot

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
     tableView.estimatedRowHeight = 500;
    return UITableViewAutomaticDimension;
}

表格单元:

for(i=0; i<subviews.count; i++)

    UIImageView *imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ic_image.png"]];

    [imgView setContentMode:UIViewContentModeScaleAspectFill];
    imgView.clipsToBounds = YES;

    [_viewAttaches addSubview:imgView];
    imgView.translatesAutoresizingMaskIntoConstraints = NO;

    if(i==0) {
        NSLayoutConstraint *top = [NSLayoutConstraint
                                       constraintWithItem:imgView
                                       attribute:NSLayoutAttributeTop
                                       relatedBy:NSLayoutRelationEqual
                                       toItem:_viewAttaches
                                       attribute:NSLayoutAttributeTop
                                       multiplier:1.0f
                                       constant:0.f];
        [_viewAttaches addConstraint:top];
     } else {
             UIImageView *prevView = subviews[i-1];
             NSLayoutConstraint *top = [NSLayoutConstraint
                                               constraintWithItem:imgView
                                               attribute:NSLayoutAttributeTop
                                               relatedBy:NSLayoutRelationEqual
                                               toItem:prevView
                                               attribute:NSLayoutAttributeBottom
                                               multiplier:1.0f
                                               constant:10.f];
              [_viewAttaches addConstraint:top];
           }

           NSLayoutConstraint *leading = [NSLayoutConstraint
                                               constraintWithItem:imgView
                                               attribute:NSLayoutAttributeLeading
                                               relatedBy:NSLayoutRelationEqual
                                               toItem:_viewAttaches
                                               attribute:NSLayoutAttributeLeading
                                               multiplier:1.0f
                                               constant:0.f];

            [_viewAttaches addConstraint:leading];

            NSLayoutConstraint *trailing = [NSLayoutConstraint
                                                constraintWithItem:imgView
                                                attribute:NSLayoutAttributeTrailing
                                                relatedBy:NSLayoutRelationEqual
                                                toItem:_viewAttaches
                                                attribute:NSLayoutAttributeTrailing
                                                multiplier:1.0f
                                                constant:0.f];

            [_viewAttaches addConstraint:trailing];


            NSLayoutConstraint *height = [NSLayoutConstraint constraintWithItem:imgView
                                                                              attribute:NSLayoutAttributeHeight
                                                                              relatedBy:NSLayoutRelationEqual
                                                                                 toItem:nil
                                                                              attribute:NSLayoutAttributeNotAnAttribute
                                                                             multiplier:1
                                                                               constant:160];
            [_viewAttaches addConstraint:height];


                if(i==nAttaches-1) {
                    NSLayoutConstraint *bottom = [NSLayoutConstraint
                                                  constraintWithItem:imgView
                                                  attribute:NSLayoutAttributeBottom
                                                  relatedBy:NSLayoutRelationEqual
                                                  toItem:_viewAttaches
                                                  attribute:NSLayoutAttributeBottom
                                                  multiplier:1.0f
                                                  constant:0.f];
                    [_viewAttaches addConstraint:bottom];
                }

                i++;
}

但是我收到了这个错误:

[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:0x174288390 UILabel:0x101157580'Daniel'.height == 21   (active)>,
"<NSLayoutConstraint:0x174288cf0 UILabel:0x101158cb0'0 comments'.height == 21   (active)>",
"<NSLayoutConstraint:0x174288e80 UILabel:0x101157580'Daniel'.top == UITableViewCellContentView:0x101157130.topMargin + 2   (active)>",
"<NSLayoutConstraint:0x174288fc0 V:[UILabel:0x101157580'Daniel']-(-1)-[UILabel:0x1011469b0'I am currently in a happy...']   (active)>",
"<NSLayoutConstraint:0x1742890b0 V:[UILabel:0x1011469b0'I am currently in a happy...']-(0)-[UIView:0x101158b10]   (active)>",
"<NSLayoutConstraint:0x174289240 V:[UIView:0x101158b10]-(-1)-[UILabel:0x101158cb0'0 comments']   (active)>",
"<NSLayoutConstraint:0x1742892e0 UITableViewCellContentView:0x101157130.bottomMargin == UILabel:0x101158cb0'0 comments'.bottom + 1   (active)>",
"<NSLayoutConstraint:0x170284ec0 UIView:0x101158b10.height == 330   (active)>",
"<NSLayoutConstraint:0x17428a640 'UIView-Encapsulated-Layout-Height' UITableViewCellContentView:0x101157130.height == 89   (active)>"


Will attempt to recover by breaking constraint 
<NSLayoutConstraint:0x170284ec0 UIView:0x101158b10.height == 330   (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.

请帮我解决我所缺少的。提前谢谢你。

【问题讨论】:

  • 你用的是storyboard还是xib?
  • stackoverflow.com/a/40035858/6656894 参考这个答案@Daniel
  • 我正在使用情节提要,@HimanshuMoradiya
  • 然后给出我在回答中给出的约束,参考上面的链接。
  • @HimanshuMoradiya 谢谢,但主要问题不是多行标签。将多图像视图插入容器视图时,约束被打破。

标签: ios objective-c uitableview autolayout


【解决方案1】:

您所做的工作几乎是正确的,请保持以下更改以解决此问题

1. 更改UITableviewCell 中的视图层次结构并在下面创建如下 .

-> Timeline TableView
  '-> postecell
     '-> content view
        '-> myView // I added this `UIview` & in this view add all you cell components
          '-> Img User
          '-> lbl Name
          :    :
          :    :

myView 约束:

  1. 顶部、底部、前导、跟踪到超级视图。

    编辑 - 删除下面的行

    1. 身高与关系greater than equal to (≥)

注意:确保单元格中的每个组件都必须有上下关系和固定高度,关系=,动态标签关系

2.你的身高代码是这样的

- (CGFloat)tableView:(UITableView *)tableView    estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
     return 250; // it is the height of cell which is in the storyboard after creation of custom cell.
}   

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return UITableViewAutomaticDimension; 
}

编辑

查看层次结构

故事板

输出:

【讨论】:

  • 我将动态高度标签(顶部、前导、尾随、>=30 高度约束)移动到容器视图(在您的示例中为 myView)。容器视图具有 >= 30 个高度约束以及超级视图的顶部、前导、尾随约束。在动态标签下方添加了 UIImageViews,它具有顶部(到上视图)、前导和尾随(到超级视图)和静态高度约束。最后一个 UIImageView 对 superview 有底部约束。但问题是一样的。
  • 请将您在 Debug View Hierarchy 中的视图层次截图发送给我,其中动态标签、容器视图、图像视图约束在树中展开。或者你能把你拥有的相同来源发给我吗?
  • 给我您的电子邮件 ID,我将向您发送演示。并且不要忘记接受并投票赞成答案。 :) :p :p
  • daniel.pelto@yahoo.com 是我的电子邮件地址。
  • @Daniel 检查您的电子邮件。如果你有什么要改变的,那就说吧。
猜你喜欢
  • 2017-07-02
  • 1970-01-01
  • 2014-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多