UIButton没有调整大小以适应它的titleLabel

时间:2017-07-05 08:49:19

标签: ios objective-c iphone ipad

我正在使用iOS8 Self-Sizing单元格,我希望单元格调整大小以适应其subviewmessageBodyButtonprofileImageViewprofileImageView的大小已修复。如下图所示,messageBodyButton没有调整大小以适应其titleLabel

为什么会这样?如何使用自动布局修复此错误?
titleLabel的内容发生变化(例如,模型的数据发生变化)时,我应该如何更新约束以便正确计算单元格高度?

pic

这是我的代码:

RXTChatCell.m

@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UIImageView *profileImageView;
@property (nonatomic, strong) UIButton *messageBodyButton;

@property (nonatomic, assign) BOOL needUpdateConstraints;
@property (nonatomic, assign) BOOL didSetUpConstraints;

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _timeLabel = [[UILabel alloc] init];
        _timeLabel.text = @"19:00";
        [self.contentView addSubview:_timeLabel];

        _profileImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"placeholder"]];
        [self.contentView addSubview:_profileImageView];

        _messageBodyButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_messageBodyButton.titleLabel setLineBreakMode:NSLineBreakByTruncatingTail];
        [_messageBodyButton.titleLabel setTextAlignment:NSTextAlignmentRight];
        _messageBodyButton.titleLabel.numberOfLines = 0;
        _messageBodyButton.titleLabel.backgroundColor = UIColor.grayColor;
        [self.contentView addSubview:_messageBodyButton];

        _timeLabel.backgroundColor = UIColor.redColor;
        _profileImageView.backgroundColor = UIColor.greenColor;
        _messageBodyButton.backgroundColor = UIColor.blueColor;
        self.contentView.backgroundColor = UIColor.orangeColor;
    }
    return self;
}

- (void)updateConstraints
{ 
    if (!self.didSetUpConstraints) { // set up constraints
        [_timeLabel makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.contentView.topMargin);
            make.centerX.equalTo(self.contentView);
        }];

        [_profileImageView makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_timeLabel.bottom).offset(10);
            make.right.equalTo(self.contentView).offset(-10);
            make.width.and.height.equalTo(50);
            make.bottom.lessThanOrEqualTo(self.contentView.bottomMargin);
        }];

        [_messageBodyButton makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_profileImageView);
            make.right.equalTo(_profileImageView.left).offset(-10);
            make.left.equalTo(self.contentView).offset(10);
            make.bottom.lessThanOrEqualTo(self.contentView.bottomMargin);
        }];
        self.didSetUpConstraints = YES;
    }

    if (self.needUpdateConstraints){
        // here, how should I update constraints?

    }

    [super updateConstraints];
}

- (void)setMessage:(RXTChatMessage *)message
{
    EMTextMessageBody *body = (EMTextMessageBody *)message.messageBody.body;
    [self.messageBodyButton setTitle:body.text forState:UIControlStateNormal];
    self.needUpdateConstraints = YES;
    [self setNeedsUpdateConstraints];
}

RXTChatViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];

    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.estimatedRowHeight = 44;
    self.tableView.rowHeight = UITableViewAutomaticDimension;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RXTChatMessage *message = self.messages[indexPath.row];
    RXTChatCell *cell = [tableView dequeueReusableCellWithIdentifier:RXTChatCellId];
    if (cell == nil) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:RXTChatCellId];
    }
    cell.message = message;

    return cell;
}

1 个答案:

答案 0 :(得分:0)

  1. 选择按钮 - >转到编辑 - >尺寸适合内容。
  2. 删除按钮的固定高度约束(仅设置前导,尾随,顶部)约束(Superview应该是您的单元格视图)。
  3. 这应该可以解决您的问题
  4. 让我知道它是否能解决您的问题。

    ]