0

UITableViewController启用自动布局的故事板中定义的一个UITableViewCell子类以编程方式创建(没有 IB 原型单元)并通过updateConstraints使用PureLayout设置约束。

出于某种原因,表格视图的每个 contentView 都有不同的宽度,而不是使用UITableView.

在此处输入图像描述

这是子类的initandupdateConstraints方法UITableViewCell-

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

        _nameLabel = [GTPaddedLabel newAutoLayoutView];
        _nameLabel.font = [BQFontUtil standardFontSize:StandardFontSize1a bold:YES italic:NO];
        _nameLabel.textColor = [UIColor blackColor];

        _drugType = [UILabel newAutoLayoutView];
        _drugType.font = [BQFontUtil standardFontSize:StandardFontSize0 bold:NO italic:NO];
        _drugType.textColor = [UIColor darkGrayColor];
        _drugType.textAlignment = NSTextAlignmentRight;

        _genericsLabel = [UILabel newAutoLayoutView];
        _genericsLabel.font = [BQFontUtil standardFontSize:StandardFontSize0 bold:NO italic:NO];
        _genericsLabel.textColor = [UIColor darkGrayColor];

        _regimen = [UILabel newAutoLayoutView];
        _regimen.font = [BQFontUtil standardFontSize:StandardFontSize0 bold:NO italic:NO];
        _regimen.textColor = [UIColor darkGrayColor];

        [self.contentView addSubview:_nameLabel];
        [self.contentView addSubview:_drugType];
        [self.contentView addSubview:_genericsLabel];
        [self.contentView addSubview:_regimen];

        self.contentView.backgroundColor = [UIColor redColor];
    }
    return self;
}

- (void)updateConstraints {
    if (!constraintsCreated) {
//        [self.contentView autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

        [UIView autoSetPriority:UILayoutPriorityRequired forConstraints:^{
            [_nameLabel autoSetContentCompressionResistancePriorityForAxis:ALAxisVertical];
        }];

        [_nameLabel autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(8, 8, 0, 8) excludingEdge:ALEdgeBottom];

        [_genericsLabel autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:16];
        [_genericsLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:_nameLabel withOffset:4];
        [_genericsLabel autoSetDimension:ALDimensionHeight toSize:21];

        [_drugType autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:16];
        [_drugType autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:_nameLabel withOffset:4];
        [_drugType autoPinEdge:ALEdgeLeading toEdge:ALEdgeTrailing ofView:_genericsLabel withOffset:8];
        [_drugType autoSetDimension:ALDimensionHeight toSize:21];

        [_regimen autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:_genericsLabel withOffset:2];
        [_regimen autoSetDimension:ALDimensionHeight toSize:21];

        [_regimen autoPinEdgeToSuperviewEdge:ALEdgeTrailing withInset:16];
        [_regimen autoPinEdgeToSuperviewEdge:ALEdgeLeading withInset:16];
        [_regimen autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:8];

        constraintsCreated = YES;
    }
    [super updateConstraints];
}

为什么 contentView (棕色)与表格视图的宽度不同?

4

1 回答 1

0

也许这是一种解决方法,当我将 contentView 的宽度限制为单元格的宽度时效果很好 -

- (void)updateConstraints {
    if (!constraintsCreated) {
        [self.contentView autoSetDimension:ALDimensionWidth toSize:self.frame.size.width];
        ...
    }  
}     
于 2014-12-10T13:26:16.227 回答