简体   繁体   English

使用自动布局(VFL)增加自定义单元格按钮的高度

[英]Increase height of custom cell button using Auto layout (VFL)

I have a custom cell in that there is a UILabel , UIButton and UIImageView . 我有一个自定义单元格,其中有一个UILabelUIButtonUIImageView

在此处输入图片说明

You can see that in the third row right hand side (which is button control - selectionButton ) shrink because button title is too long to fit in. 您会看到在第三行右侧(按钮控件selectionButton )缩小,因为按钮标题太长而无法放入。

The specific requirement here is to increase the height of button and cell height based on long or short title of that button. 此处的特定要求是根据按钮的长标题或短标题来增加按钮的高度和单元格的高度。

After wasting many hours I couldn't figure out constraints. 浪费了很多时间后,我无法找出限制。

Below are current constraint for above custom cell arrangement: 以下是上述自定义单元格排列的当前约束条件:

// Constraints
    NSDictionary *views = @{@"titleLabel" : self.titleLabel, @"selectionButton" : self.selectionButton, @"arrowImage" : self.arrowImage};

    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-15-[titleLabel]-15-|" options: 0 metrics:nil views:views]];

    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[selectionButton]|" options:0 metrics:nil views:views]];

    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-20-[arrowImage(10)]" options:0 metrics:nil views:views]];

    [self.contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-15-[titleLabel]-2-[selectionButton]-5-[arrowImage(10)]-10-|" options: 0 metrics:nil views:views]];

    NSLayoutConstraint *constraint1 = [NSLayoutConstraint constraintWithItem:self.titleLabel attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:self.contentView attribute:NSLayoutAttributeWidth multiplier:0.55 constant:0];
    [self.contentView addConstraint:constraint1];

    NSLayoutConstraint *constraint2 = [NSLayoutConstraint constraintWithItem:self.selectionButton attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationLessThanOrEqual toItem:self.contentView attribute:NSLayoutAttributeWidth multiplier:0.45 constant:0];
    [self.contentView addConstraint:constraint2];

Your constraints looks fine to me if you're doing: 如果您这样做,对我来说您的约束看起来很好:

  • making your tableview.rowHeight = UITableViewAutomaticDimension 使您的tableview.rowHeight = UITableViewAutomaticDimension
  • making selectionButton titleLabel allows multiple lines 进行selectionButton titleLabel允许多行

     self.selectionButton.titleLabel.numberOfLines = 0; self.selectionButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM