UICollectionViewSplitLayout
UICollectionViewSplitLayout makes collection view more responsive.
What's this?
UICollectionViewSplitLayout is a subclass of UICollectionViewLayout. It divides sections into one or two column.
Collection view has "Section" which organizes item collection.
UICollectionViewFlowLayout layouts them from top to bottom.
On the other hands, UICollectionViewSplitLayout divides sections into two columns.
You can dynamically update the width of them and which column each section is on.
For example, a collection view with UICollectionViewSplitLayout changes the number of column according to device orientation. All you need is assign value to leftSideRatio
when changing screen size. This figure describes a collection view has three sections (red, blue and green) and UICollectionViewSplitLayout layouts them in left or right side.
It may be hard to imagine how it works, please run EmojiCollectionViewController.
Requirement
- iOS 9.0+
- Swift 4.2
Installation
Carthage
1. create Cartfile
2. install
> carthage update
CocoaPods
1. create Podfile
2. install
> pod install
Getting Started
It’s good to start from replacing UICollectionViewFlowLayout with UICollectionViewSplitLayout.
1. Create UICollectionViewController
Set UICollectionViewController on Storyboard.

Add implementation to construct a collection view.
Build the code.

It shows three sections whose items have different colors.
2. Input "UICollectionViewSplitLayout" into Custom Layout Class
Change the attribute of Layout into "Custom" and input "UICollectionViewSplitLayout" into Class and Module attribute.

3. Assign parameters to UICollectionViewSplitLayout object
Connect layout class in storyboard to source code. Assign the parameters on viewDidLoad()
4. Implement the layout delegate for UICollectionViewSplitLayout
Implement UICollectionViewDelegateTwoColumnLayout. The following methods is required.
It divides each section into left or right.

See BasicCollectionViewController to run the above example.
Architecture
It is one of the UICollectionViewLayout. So you can change a layout without updating code in UICollectionViewDelegate and UICollectionViewDataSource. It is reasonable to apply a new layout. All you have to do is studying layout class and the delegate.
Usage
How to Split
UICollectionViewSplitLayout calculates width of the left and right side with the following parameter.
It has a Float value which is the ration of left side to the entire width.
In addition, left and right sides have spacing between them.
If a collection view has 110pt width, the above setting requires 40pt width to the left side and 60pt width to the right side.
When leftSideRatio has 0.0, splitSpacing is ignored as an exception.
You can choose which side each section is in. UICollectionViewDeleagteSectionSplitLayout provides a method to do that.
UICollectionViewSplitLayout hooks the delegate every time a collection view calls invalidateLayout().
Margins
UICollectionViewSplitLayout has these parameters to determine margins.
They have the corresponding delegate methods by section (optional).
Pinning (iOS 11~)
UICollectionViewSplitLayout pins seciton header like UICollectionViewFlowLayout. This feature is supported from iOS 11. sectionPinToVisibleBounds
enables to work it.
Setting Attriutes
UICollectionViewSplitLayout uses three kinds of attribute, item, supplementary view and decoration view.
Item
You need to implement cell sizing. UICollectionViewSplitLayout provides a delegate to implement it.
You can divide the sizes evenly with a utility method.
Supplementary view for header and footer
You can implement header and footer sizing. UICollectionViewSplitLayout provides delegate to implement it.
If the sizes are zero, header and footer are ignoured.
decoration view for backgroundColor
UICollectionSplitLayout has a special decoration view. It has the same size as the section. You can determine backgroundColor to each section with the following delegate.
Line Height Normalization
If This porperty is true, item height is adjusted to outline height of the line (defalut: false).