Demo for Collection View on iOS with auto layout in UICollectionViewCell to create cells with dynamic heights
Works on iOS7 and iOS8
Cell is created in xib file, can be also created in code.
Cell is a basic cell with a title label and a content label. Title label has 1 line text and content label has multiple lines text.
There are totally 7 constraints for two labels:
For title label, there are top, leading, trailing spacing with superView. For content label, there are botton, leading, trailing spacing with superView. And there is a fixed vertical spacing between bottom of title label with top of content label
All of these 7 constraints have 1000 priority
For two labels, set their Content Hugging Priority and Content Compression Resistance Priority same as following pic:
Subclassing this UICollectionViewCell
In awakeFromNib()
, for iOS7 remember to set
self.contentView.autoresizingMask = UIViewAutoresizing.FlexibleHeight | UIViewAutoresizing.FlexibleWidt
and in layoutSubviews()
add contentLabel.preferredMaxLayoutWidth = self.bounds.width - 2 * kLabelHorizontalInsets