- August 28, 2025
- Mins Read
A mosaic collection view layout inspired by Lightbox’s Algorithm. This is a swift implementation/extension of @fmitech’s FMMosaicLayout. The standard UICollectionViewFlowLayout
can be a boring presentation in your app, but TRMosaicLayout is a simple feature that will engage your users with your eyecatching content. If you don’t believe me, checkout Snapchat’s app and their awesome layout in their Discover feed.
TRMosaicLayout implementation
Similar implementation used by @snapchat
You can use CocoaPods to install TRMosaicLayout
by adding it to your Podfile
:
platform :ios, ‘8.0’
use_frameworks!
pod ‘TRMosaicLayout’
Then, import TRMosaicLayout
import TRMosaicLayout
/TRMosaicLayout
folder in your project.Create a subclass of UICollectionViewController
import TRMosaicLayout
class TRCollectionViewController: UICollectionViewController {
override func viewDidLoad() {
super.viewDidLoad()
let mosaicLayout = TRMosaicLayout()
self.collectionView?.collectionViewLayout = mosaicLayout
mosaicLayout.delegate = self
}
}
Extend your subclass of UICollectionViewController
with TRMosaicLayoutDelegate
extension TRCollectionViewController: TRMosaicLayoutDelegate {
func collectionView(collectionView:UICollectionView, mosaicCellSizeTypeAtIndexPath indexPath:NSIndexPath) -> TRMosaicCellType {
// I recommend setting every third cell as .Big to get the best layout
return indexPath.item % 3 == 0 ? TRMosaicCellType.Big : TRMosaicCellType.Small
}
func collectionView(collectionView:UICollectionView, layout collectionViewLayout: TRMosaicLayout, insetAtSection:Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 3, left: 3, bottom: 3, right: 3)
}
func heightForSmallMosaicCell() -> CGFloat {
return 150
}
}
The cell’s aren’t aligned properly
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)
let imageView = UIImageView(image: image)
imageView.frame = cell.frame
cell.backgroundView = imageView
return cell
nil while unwrapping
errorTRMosaicLayout
to your collectionViewController
mosaicLayout.delegate = self
This package provides you with an easy way to show tooltips over any SwiftUI view, since Apple does not provide ...
SimpleToast is a simple, lightweight, flexible and easy to use library to show toasts / popup notifications inside iOS or ...
Create Toast Views with Minimal Effort in SwiftUI Using SSToastMessage. SSToastMessage enables you to effortlessly add toast notifications, alerts, and ...