UITableViewCell 自动布局崩溃

UITableViewCell Auto Layout crashed

我有下面的代码来简单地将 UILabel 放在自定义 UITableViewCell 中。我想要实现的是标签在 table 单元格的 left/right 侧有 20 个边距。它应该是非常基本的,但它会使应用程序崩溃:

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

        self.label = [[UILabel alloc] init];
        self.label.translatesAutoresizingMaskIntoConstraints = false;

        [self.label.leftAnchor constraintEqualToAnchor:self.contentView.leftAnchor constant:20].active = YES;
        [self.label.rightAnchor constraintEqualToAnchor:self.contentView.rightAnchor constant:20].active = YES;
        [self.label.topAnchor constraintEqualToAnchor:self.contentView.topAnchor constant:0].active = YES;
        [self.label.bottomAnchor constraintEqualToAnchor:self.contentView.bottomAnchor constant:0].active = YES;

        [self.contentView addSubview:self.label];
    }

    return self;
}

请帮我找出我做错了什么。谢谢!

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

        self.label = [[UILabel alloc] init];
        self.label.translatesAutoresizingMaskIntoConstraints = false;

        [self.contentView addSubview:self.label];

        [self.label.leftAnchor constraintEqualToAnchor:self.contentView.leftAnchor constant:20].active = YES;
        [self.label.rightAnchor constraintEqualToAnchor:self.contentView.rightAnchor constant:20].active = YES;
        [self.label.topAnchor constraintEqualToAnchor:self.contentView.topAnchor constant:0].active = YES;
        [self.label.bottomAnchor constraintEqualToAnchor:self.contentView.bottomAnchor constant:0].active = YES;
    }

    return self;
}

这会起作用