Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove UILabel padding/margin while using auto layout

I have a UITableView that uses auto layout. I have multiple types of cells, but for now let's use the simplest of all, one that only has a UILabel.

In tableView:heightForRowAtIndexPath: I have the following code:

    [self configureCell:self.detailsCell forRowAtIndexPath:indexPath];
    [self.detailsCell layoutIfNeeded];

    CGSize size = [self.detailsCell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
    return size.height + 1;

which in turn calls

- (void)configureCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
    // ...
    cell.detailsLabel.text = self.data.myText;
    cell.detailsLabel.font = [UIFont preferredFontForTextStyle:UIFontTextStyleBody];
    // [cell.detailsLabel sizeToFit]; <- I tried this, but didn't help
}

Interface builder screenshot

and I end up getting this:

Actual cell

The green rectangle is the actual cell. The purple rectangle is the UILabel. I don't want the top and bottom purple padding/margin. How can I get rid of it?

like image 608
Eric Avatar asked Sep 30 '22 16:09

Eric


1 Answers

you have to be beware of "Constrain to margins:"

compare these two images: Constrain to margins = YES and Constrain to marings = NO

so unselecting "constrian to margins" while adding new constraints, should solve your padding/margin problem :-)

If you like, you can visualize these margins with "Editor/Canvas/Show Layout Rectangles" The View without the layout rectangles: Show Layout rectangles = NO and layout rectangles set to show (which illustrates your problem quite good) Show Layout rectangles = YES

like image 112
Flori Avatar answered Oct 03 '22 06:10

Flori