2024-07-17

2024-07-16  本文已影响0人  时光流逝_6602

此代码通过设置自动布局约束条件来确保 timeLabel 始终完全显示,并且与 titleLabel 保持 12 点的间距。通过调整内容抗拉伸优先级和内容压缩抗拉伸优先级,确保 timeLabel 优先显示, titleLabel 根据剩余空间调整其宽度。

    //标题

    [self.contentView addSubview:self.titleLabel];

    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.mas_equalTo(self.iconImageView.mas_right).mas_offset(SLMargin);

        make.top.mas_equalTo(SLMargin);

        make.right.mas_equalTo(self.timeLabel.mas_left).mas_offset(-12);

    }];

    //时间

    [self.contentView addSubview:self.timeLabel];

    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {

        make.right.mas_equalTo(-SLMargin);

        make.centerY.mas_equalTo(self.titleLabel);

        make.left.mas_greaterThanOrEqualTo(self.titleLabel.mas_right).mas_offset(12);

    }];

    [self.titleLabel setContentHuggingPriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];

    [self.titleLabel setContentCompressionResistancePriority:UILayoutPriorityDefaultLow forAxis:UILayoutConstraintAxisHorizontal];

    [self.timeLabel setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];

    [self.timeLabel setContentCompressionResistancePriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisHorizontal];

上一篇 下一篇

猜你喜欢

热点阅读