I am currently implementing a UICollectionView
with several sections (let's say 4) like in the image below:
From section 0 to section 2 it is just a UICollectionViewFlowLayout
with different cell sizes for each section but for section 3, it is a custom layout (Waterfall layout).
I already have implemented the 2 different layouts and they work well in separate UICollectionView
, but I have some trouble to switch between the 2 layouts in the same UICollectionView
.
First of all is it possible to change the layout from a section to another and if it is by which way this can be accomplished.
You need to implement a custom UICollectionViewLayout. With a horizontal flow layout it's going to fill from top to bottom first, then move to the right. Since you have two rows, as specified in sizeForItemAt, section 0 will fill from top to bottom, then right to left, and so will section 1.
To lay out UICollectionView cells in a simple grid, you can use UICollectionViewFlowLayout directly. For more flexibility, you can subclass UICollectionViewLayout to create advanced layouts.
There are no section headers in the UICollectionView. So for your first task, you'll add a new section header using the search text as the section title. To display this section header, you'll use UICollectionReusableView .
I think your question header is a bit misleading. You don't need to change the layout for each section. You need to show different layouts depending on section.
To achieve what you want you must subclass UICollectionViewLayout
and then determine the layout depending on section. In your case I suggest you to subclass UICollectionViewFlowLayout
as it takes a lot of heavy lifting.
Section 0 - Section 2
of your sample are easily achievable by using just UICollectionViewDelegateFlowLayout
.
As you have "full width" cells there, than you can determine each cell size and insets using the following methods:
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
insetForSectionAt section: Int) -> UIEdgeInsets
func collectionView(UICollectionView, layout: UICollectionViewLayout, sizeForItemAt: IndexPath) -> CGSize
func collectionView(UICollectionView, layout: UICollectionViewLayout, minimumLineSpacingForSectionAt: Int) -> CGFloat
First problem will appear when you will try to build Section 3. For that case I suggest you to search for "Waterfall layout", there are implementations on GitHub. When you will figure out how it works, you should do the following:
UICollectionView
UICollectionViewFlowLayout
subclassUICollectionViewFlowLayout
possibilities. func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes?
and calculate attributes manually.Sorry, if my answer is too broad.
Here are some useful links:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With