具有自动布局约束的UITableViewCell子类大小不正确

乔丹·H

我正在尝试以编程方式创建一个自定义UITableViewCell(没有XIB),该自定义包含一个UILabel使用“自动布局”来指定单元格高度的单个我已将此标签赋予单元格的前导,尾随,顶部和底部约束,contentView但是这些约束不会影响表格视图单元的高度。标签全部堆叠在一起而没有填充,并且表格视图分隔线也不与标签对齐。这里有什么问题?

在此处输入图片说明

自定义单元:

@interface TransactionTableViewCell ()

@property (nonatomic, assign) BOOL didUpdateConstraints;

@end


@implementation TransactionTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.translatesAutoresizingMaskIntoConstraints = NO;

        self.transactionPriceLabel = [[UILabel alloc] init];
        self.transactionPriceLabel.backgroundColor = [UIColor yellowColor];
        self.transactionPriceLabel.translatesAutoresizingMaskIntoConstraints = NO;
        [self.contentView addSubview:self.transactionPriceLabel];
    }

    [self setNeedsUpdateConstraints]; //had to add this otherwise updateConstraints isn't called for some reason

    return self;

}

- (void)updateConstraints {
    if (!self.didUpdateConstraints) {
        self.didUpdateConstraints = YES;

        [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.transactionPriceLabel
                                                                     attribute:NSLayoutAttributeTrailing
                                                                     relatedBy:NSLayoutRelationEqual
                                                                        toItem:self.contentView
                                                                     attribute:NSLayoutAttributeTrailing
                                                                    multiplier:1
                                                                      constant:-15]];
        [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.transactionPriceLabel
                                                                     attribute:NSLayoutAttributeTop
                                                                     relatedBy:NSLayoutRelationEqual
                                                                        toItem:self.contentView
                                                                     attribute:NSLayoutAttributeTop
                                                                    multiplier:1
                                                                      constant:15]];
        [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.transactionPriceLabel
                                                                     attribute:NSLayoutAttributeBottom
                                                                     relatedBy:NSLayoutRelationEqual
                                                                        toItem:self.contentView
                                                                     attribute:NSLayoutAttributeBottom
                                                                    multiplier:1
                                                                      constant:15]];
        [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.transactionPriceLabel
                                                                     attribute:NSLayoutAttributeLeading
                                                                     relatedBy:NSLayoutRelationEqual
                                                                        toItem:self.contentView
                                                                     attribute:NSLayoutAttributeLeading
                                                                    multiplier:1
                                                                      constant:15]];
    }

    [super updateConstraints];
}

查看控制器:

- (void)viewDidLoad {
    UITableView *tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    tableView.dataSource = self;
    tableView.delegate = self;
    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.estimatedRowHeight = 44.0;
    [self.view addSubview:tableView];

    tableView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:tableView
                                                     attribute:NSLayoutAttributeLeading
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:self.view
                                                     attribute:NSLayoutAttributeLeading
                                                    multiplier:1
                                                      constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:tableView
                                                     attribute:NSLayoutAttributeTop
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:self.view
                                                     attribute:NSLayoutAttributeTop
                                                    multiplier:1
                                                      constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:tableView
                                                     attribute:NSLayoutAttributeBottom
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:self.view
                                                     attribute:NSLayoutAttributeBottom
                                                    multiplier:1
                                                      constant:0]];
    [self.view addConstraint:[NSLayoutConstraint constraintWithItem:tableView
                                                     attribute:NSLayoutAttributeTrailingMargin
                                                     relatedBy:NSLayoutRelationEqual
                                                        toItem:self.view
                                                     attribute:NSLayoutAttributeTrailingMargin
                                                    multiplier:1
                                                      constant:0]];

    [tableView registerClass:[TransactionTableViewCell class] forCellReuseIdentifier:@"cell"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    TransactionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];

    cell.transactionPriceLabel.text = @"label text";
    cell.transactionPriceLabel.textColor = [UIColor redColor];

    return cell;
}
德尔玛

标签底部到contentView约束底部的常数应为-15,而不是15。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

具有自动布局的UITableViewCell中动态调整大小的UIWebView-违反约束

来自分类Dev

具有自动布局的UITableViewCell中动态调整大小的UIWebView-违反约束

来自分类Dev

具有自动布局的UITableViewCell-contentView宽度约束失败

来自分类Dev

约束后堆栈大小不正确

来自分类Dev

iOS 自动布局结果不正确

来自分类Dev

具有多个离线约束的ALTER TABLE的铁路图不正确?

来自分类Dev

具有“回调类型”的TypeScript索引类型约束工作不正确?

来自分类Dev

具有相对约束的自动布局不会影响systemLayoutSizeFittingSize:用于UITableViewCell

来自分类Dev

iOS 上的 UITableViewCell 自动高度不正确

来自分类Dev

具有自动布局的Swift UIView子类

来自分类Dev

自动布局:使用ScrollView时宽度不正确

来自分类Dev

自动布局:UIView中的UIView框架不正确

来自分类Dev

自动布局-视图控制器框架不正确

来自分类Dev

约束动画不正确

来自分类Dev

如何计算从具有自动布局纵横比约束的xib加载的UICollectionViewCell的大小?

来自分类Dev

如何计算从具有自动布局纵横比约束的xib加载的UICollectionViewCell的大小?

来自分类Dev

绘制具有多个组件的图形时,节点大小不正确

来自分类Dev

SimpleModal自动调整大小并不正确

来自分类Dev

自动版式:ViewDidAppear中的帧大小不正确

来自分类Dev

SimpleModal自动调整大小并不正确

来自分类Dev

自动版式:ViewDidAppear中的帧大小不正确

来自分类Dev

在具有自动布局的UIView子类上正确使用internalContentSize和sizeThatFits:

来自分类Dev

布局渲染不正确

来自分类Dev

ContentPresenter布局不正确

来自分类Dev

缩进UITableViewCell中的所有自动布局约束

来自分类Dev

AvPlayerLayer大小不正确?

来自分类Dev

JFrame大小不正确

来自分类Dev

iframe大小不正确

来自分类Dev

UIView大小不正确

Related 相关文章

  1. 1

    具有自动布局的UITableViewCell中动态调整大小的UIWebView-违反约束

  2. 2

    具有自动布局的UITableViewCell中动态调整大小的UIWebView-违反约束

  3. 3

    具有自动布局的UITableViewCell-contentView宽度约束失败

  4. 4

    约束后堆栈大小不正确

  5. 5

    iOS 自动布局结果不正确

  6. 6

    具有多个离线约束的ALTER TABLE的铁路图不正确?

  7. 7

    具有“回调类型”的TypeScript索引类型约束工作不正确?

  8. 8

    具有相对约束的自动布局不会影响systemLayoutSizeFittingSize:用于UITableViewCell

  9. 9

    iOS 上的 UITableViewCell 自动高度不正确

  10. 10

    具有自动布局的Swift UIView子类

  11. 11

    自动布局:使用ScrollView时宽度不正确

  12. 12

    自动布局:UIView中的UIView框架不正确

  13. 13

    自动布局-视图控制器框架不正确

  14. 14

    约束动画不正确

  15. 15

    如何计算从具有自动布局纵横比约束的xib加载的UICollectionViewCell的大小?

  16. 16

    如何计算从具有自动布局纵横比约束的xib加载的UICollectionViewCell的大小?

  17. 17

    绘制具有多个组件的图形时,节点大小不正确

  18. 18

    SimpleModal自动调整大小并不正确

  19. 19

    自动版式:ViewDidAppear中的帧大小不正确

  20. 20

    SimpleModal自动调整大小并不正确

  21. 21

    自动版式:ViewDidAppear中的帧大小不正确

  22. 22

    在具有自动布局的UIView子类上正确使用internalContentSize和sizeThatFits:

  23. 23

    布局渲染不正确

  24. 24

    ContentPresenter布局不正确

  25. 25

    缩进UITableViewCell中的所有自动布局约束

  26. 26

    AvPlayerLayer大小不正确?

  27. 27

    JFrame大小不正确

  28. 28

    iframe大小不正确

  29. 29

    UIView大小不正确

热门标签

归档