Reputation: 2206
How do I vertically align the cells of my uicollectionview to the top in swift?
I've seen people subclassing them in objective-c but i haven't found one answer in swift.
Thank you
Upvotes: 2
Views: 4940
Reputation: 3016
I used the library provided but I think a new answer is warranted in that the last one was pretty incomplete for those not 100% familiar with how custom layouts work with UICollectionViews.
Step 1 - import the two files FSQCollectionViewAlignedLayout.h
and FSQCollectionViewAlignedLayout.m
(or put pod 'FSQCollectionViewAlignedLayout'
in your podfile and run pod install
)
Step 2 - Add them to your bridging header because you're probably using Swift. (Not necessary if you imported as a pod)
Step 3 - In your storyboard, change the "Layout" attribute in Attributes inspector to Custom, then add your custom class (FSQCollectionViewAlignedLayout) in that field.
Step 4 - Add the delegate methods to your ViewController and edit properties as needed. Also make sure to import FSQCollectionViewAlignedLayout
at the top of your ViewController class.
Upvotes: 1
Reputation: 2206
I ended up using FSQCollectionView which was perfect for my needs.
The project is availble here :
https://github.com/foursquare/FSQCollectionViewAlignedLayout
And the two methods needed were:
func collectionView(collectionView: UICollectionView!, layout collectionViewLayout: FSQCollectionViewAlignedLayout!, attributesForSectionAtIndex sectionIndex: Int) -> FSQCollectionViewAlignedLayoutSectionAttributes! {
return FSQCollectionViewAlignedLayoutSectionAttributes.withHorizontalAlignment(FSQCollectionViewHorizontalAlignment.Center, verticalAlignment: FSQCollectionViewVerticalAlignment.Top)
}
and
func collectionView(collectionView: UICollectionView!, layout collectionViewLayout: FSQCollectionViewAlignedLayout!, sizeForItemAtIndexPath indexPath: NSIndexPath!, remainingLineSpace: CGFloat) -> CGSize {
return CGSizeMake(100, 100)
}
Upvotes: 3