Mehul Patel
Mehul Patel

Reputation: 23053

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

I have a custom cell in that there is a UILabel, UIButton and UIImageView.

enter image description here

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.

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];

Upvotes: 1

Views: 186

Answers (1)

Ismail
Ismail

Reputation: 2818

Your constraints looks fine to me if you're doing:

  • making your tableview.rowHeight = UITableViewAutomaticDimension
  • making selectionButton titleLabel allows multiple lines

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

Upvotes: 1

Related Questions