XCode Warrier
XCode Warrier

Reputation: 775

Stretch UILabel to full width of UICollectionViewCell

This sounds like a trivial thing to do, but for some reason I can't figure out how to get this work with AutoLayout.

I have a UICollectionViewCell with two subviews in Interface Builder

UICollectionViewCell
  - UILabel
  - UILabel

The UICollectionViewCell has a dynamic width height, and so I'd like to set the first UILabel's frame to be something like this using AutoLayout.

CGRectMake(5, 5, collectionViewCell width, collectionViewCell height-10);

However if I select UICollectionViewCell and UILabel, and then click the left-most icon to "Add new alignment constraints", then none of the options are available (e.g. Trailing Edges).

Upvotes: 0

Views: 418

Answers (1)

user4151918
user4151918

Reputation:

Those menu items were not enabled, because the two items you selected did not have a common superview.

Select UILabel without also selecting UICollectionViewCell.

Auto Layout already understands that the cell is the label's superview, and knows to pin the label's edges to the cell.

Update:

  • The 'Pin' tool is used for size, or spacing between items. In your case, you want to set the space between your label and the cell.

  • The 'Align' tool is used for centering items, or lining items up by an edge. For example, if you had two labels in your cell, you could ensure that one label lined up with the other label's baseline.

Upvotes: 1

Related Questions