I wanted to make a UICollectionViewCell which resizes itself based on the content in it (for now it has a UILabel which can have a text of varied length). There are some sollutions on the web but they do not work always.
Please help me find a sollution to this.
I have used an estimatedItemSize property for the layout of the collection view
ex:
viewDidLoad(){
collectionView.dataSource = self
collectionView.delegate = self
let layout = UICollectionViewFlowLayout()
layout.minimumInteritemSpacing = 5.0
layout.minimumLineSpacing = 5.0
layout.estimatedItemSize = CGSize(width: 30, height: 21)
/
collectionView.collectionViewLayout = layout
}
inside the :cellForItemAtIndexPath:
/
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
let screenHeight = screenSize.height
let usableScreenWidth = screenWidth * 0.50
let maxLayoutWidthForLable = usableScreenWidth * (3.0/4.0)
let maxLayoutWidthForButton = usableScreenWidth * (1.0/4.0)
/
cell.labelView.preferredMaxLayoutWidth = maxLayoutWidthForLable
cell.buttonView.titleLabel?.preferredMaxLayoutWidth = maxLayoutWidthForButton
//just allowing the prefered sizes for the label and button
and on the storyboard the
button and the label are allowed to fill the prototype and the number of lines for the label is set to 0
Thanks
santosh