Remove UILabel Space / Marker When Using Auto Layout

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

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 causes

 - (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 get the following:

Actual cell

The green rectangle is the actual cell. Purple Rectangle - UILabel. I don't want upper and lower purple indentation / margin. How can I get rid of it?

+5
source share
3 answers

you should beware of " Margin Limit: "

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

so disabling "constrian to margins" when adding new restrictions should solve your fill / field problem :-)

If you like, you can visualize these fields using "Editor / Canvas / Show Layout Rectangles", View without layout rectangles: Show Layout rectangles = NO and layout rectangles set to show (which illustrates your problem well) Show Layout rectangles = YES

+5
source

You can add a horizontal center to UILabel. Then set the “Great or Equal” ratio of “upper space to supervisory” and set the lower space in the same way. Build and run, and you will see that the dynamic height of the UILabel matches the contents of the UILabel.

0
source

Fields due to autodetection restrictions that you have configured. Set all fields to zero in the size inspector.

If you use Xcode 6 and support iOS 8, restrictions can be set as "container margin" instead of "container". This is a little different and setting the field in the container to zero will work for you. You can switch between them when setting constraints in the storyboard by pressing Alt.

-1
source

Source: https://habr.com/ru/post/1204381/


All Articles