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
}
and I end up getting this:
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?
you have to be beware of "Constrain to margins:"
compare these two images: and
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: and layout rectangles set to show (which illustrates your problem quite good)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With