A UICollectionViewFlowLayout subclass that scales items to take up space, optimized for large item set, inspired by NHBalancedFlowLayout.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.

README.md

ZLBalancedFlowLayout

A UICollectionViewFlowLayout subclass that scales items to take up space, optimized for large item set, inspired by NHBalancedFlowLayout.

Previews

###UICollectionViewScrollDirectionVertical ###UICollectionViewScrollDirectionHorizontal

CocoaPods

You can install ZLBalancedFlowLayout through CocoaPods adding the following to your Podfile:

pod 'ZLBalancedFlowLayout'

CocoaPods' support for swift is still pre-released, and requires your iOS deployment target to be 8.0 or later:

[sudo] gem install cocoapods --pre

Usage

Checkout the demo app for an example.

ZLBalancedFlowLayout supports all properties of UICollectionViewFlowLayout and the corresponding delegate methods, including minimumLineSpacing, minimumInteritemSpacing, sectionInset, etc.

Use rowHeight to adjust the item height.

/// The ideal row height of items in the grid
var rowHeight = CGFloat(100)

/// The option to enforce the ideal row height by changing the aspect ratio of the item if necessary.
var enforcesRowHeight = false

Benchmarks

ZLBalancedFlowLayout uses dynamic programming techniques to find the optimal partition of cells upfront. The benchmarks are based on timings of prepareLayout:

###iPhone 6 NumSections|NumItemsPerSection|ZLBalancedFlowLayout|UICollectionViewFlowLayout ---|---|---|--- 1 | 100 | 0.0517209768295288 | 0.00196903944015503 1 | 1000 | 0.565874993801117 | 0.0149750113487244 1 | 3000 | Recursion overflow, not yet tail optimized | 0.0408549904823303 10 | 100 | 0.523442983627319 | 0.0157199501991272 10 | 1000 | 5.28750801086426 | 0.130671977996826 100 | 100 | 4.89241003990173 | 0.118210017681122

###iOS simulator NumSections|NumItemsPerSection|ZLBalancedFlowLayout|UICollectionViewFlowLayout ---|---|---|--- 1|100|0.0160560011863708|0.000555038452148438 1|1000|0.228259027004242|0.0039139986038208 1|3000|1.20643001794815|0.0141730308532715 10|100|0.158486008644104|0.0039370059967041 10|1000|2.26102298498154|0.0399309992790222 100|100|1.60317003726959|0.0364070534706116 100|1000|21.678355038166|0.385994970798492

Requirements

  • iOS 6 or higher.
  • Automatic Reference Counting (ARC).

License

ZLBalancedFlowLayout is available under MIT license. See the LICENSE file for more info.

Photo Credits

The photos in the demo app are selected from National Geographic