Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Tom Coomer
1,331 PointsChanging layout of UICollectionView for each item
I have the code below in my viewDidLoad method.
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 70, left: 10, bottom: 60, right: 10)
layout.itemSize = CGSize(width: self.view.frame.width - 20, height: 100)
collectionView = UICollectionView(frame: CGRectMake(0, 75, self.view.frame.width, self.view.frame.height-75), collectionViewLayout: layout)
collectionView!.dataSource = self
collectionView!.delegate = self
collectionView!.registerClass(ChatCellCollectionViewCell.self, forCellWithReuseIdentifier: "Cell")
collectionView!.backgroundColor = UIColor.clearColor()
collectionView!.alwaysBounceVertical = true
self.view.addSubview(collectionView!)
Each item has a UILabel that has the text set from an array. This then determines the height of the UILabel. How can I also resize the height of the items in the collection view to the same as the label? Thanks
1 Answer
Stephen Whitfield
16,771 PointsHave you tried implementing the UICollectionViewDelegateFlowLayout delegate method 'sizeForItemAtIndexPath" ?
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
return CGSizeMake(label.size.width, label.size.height);
}