【问题标题】:Custom UITableViewCell auto layout doesn't work自定义 UITableViewCell 自动布局不起作用
【发布时间】:2019-11-30 23:25:26
【问题描述】:

我正在尝试以编程方式使用自动布局实现自定义表格单元格,如下所示,但由于某种原因,我没有得到预期的结果。

预期

实际

我的观察是:

  • 单元格高度不随内容增长而增长,内容溢出;
  • bar 元素应该是一个垂直的蓝色条,但它没有正确显示;
  • 由于某种原因,在 UIView 元素上设置背景颜色根本不起作用。

请分享一些关于我做错了什么的指示。提前致谢

UITableViewCell 代码如下

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

        self.infoContainer = [[UIView alloc] init];
        self.title = [[UILabel alloc] init];
        self.time = [[UILabel alloc] init];
        self.bar = [[UIView alloc] init];

        [self.infoContainer addSubview:self.bar];
        [self.infoContainer addSubview:self.title];
        [self.infoContainer addSubview:self.time];
        [self.contentView addSubview:self.infoContainer];

        self.infoContainer.translatesAutoresizingMaskIntoConstraints = NO;
        self.title.translatesAutoresizingMaskIntoConstraints = NO;
        self.time.translatesAutoresizingMaskIntoConstraints = NO;
        self.bar.translatesAutoresizingMaskIntoConstraints = NO;

        [self.infoContainer.leftAnchor constraintEqualToAnchor:self.contentView.leftAnchor constant:18].active = YES;
        [self.infoContainer.rightAnchor constraintEqualToAnchor:self.contentView.rightAnchor constant:-18].active = YES;
        [self.infoContainer.topAnchor constraintEqualToAnchor:self.contentView.topAnchor constant:10].active = YES;
        self.infoContainer.backgroundColor = [UIColor yellowColor];

        [self.bar.leftAnchor constraintEqualToAnchor:self.infoContainer.leftAnchor constant:0].active = YES;
        [self.bar.topAnchor constraintEqualToAnchor:self.infoContainer.topAnchor constant:0].active = YES;
        [self.bar.bottomAnchor constraintEqualToAnchor:self.infoContainer.bottomAnchor constant:0].active = YES;
        [self.bar.heightAnchor constraintEqualToAnchor:self.infoContainer.heightAnchor].active = YES;
        [self.bar.widthAnchor constraintEqualToConstant:10];
        self.bar.backgroundColor = [UIColor blueColor];

        [self.title.leftAnchor constraintEqualToAnchor:self.bar.rightAnchor constant:15].active = YES;
        [self.title.rightAnchor constraintEqualToAnchor:self.infoContainer.rightAnchor constant:0].active = YES;
        [self.title.topAnchor constraintEqualToAnchor:self.infoContainer.topAnchor constant:0].active = YES;

        [self.time.leftAnchor constraintEqualToAnchor:self.title.leftAnchor constant:0].active = YES;
        [self.time.rightAnchor constraintEqualToAnchor:self.title.rightAnchor constant:0].active = YES;
        [self.time.topAnchor constraintEqualToAnchor:self.title.bottomAnchor constant:10].active = YES;
    }

    return self;
}

在表格视图中,我有:

self.recentView.rowHeight = UITableViewAutomaticDimension;
self.recentView.estimatedRowHeight = 64.0f;

谢谢!

【问题讨论】:

    标签: ios objective-c uitableview autolayout


    【解决方案1】:

    自动高度完全取决于从上到下正确挂钩约束,所以你错过了 2 个约束

    1-

    [self.infoContainer.bottomAnchor constraintEqualToAnchor:self.contentView.bottomAnchor constant:10].active = YES;
    

    2-

    [self.time.bottomAnchor constraintEqualToAnchor:self.infoContainer.bottomAnchor constant:10].active = YES;
    

    提示:如果您在此处设置顶部和底部约束

    [self.bar.topAnchor constraintEqualToAnchor:self.infoContainer.topAnchor constant:0].active = YES;
    [self.bar.bottomAnchor constraintEqualToAnchor:self.infoContainer.bottomAnchor constant:0].active = YES;
    

    那就不用了

    [self.bar.heightAnchor constraintEqualToAnchor:self.infoContainer.heightAnchor].active = YES;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-05
      • 2017-05-29
      • 2023-04-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多