Slide 1

Slide 1 text

Backporting Collection View Compositional Layouts Kishikawa Katsumi

Slide 2

Slide 2 text

Agenda • What is Collection View Compositional Layouts? • What problem does it solve? • Anatomy: Compositional Layouts • Backporting Collection View Compositional Layouts

Slide 3

Slide 3 text

What is Collection View Compositional Layouts?

Slide 4

Slide 4 text

What is Collection View Compositional Layouts? • New API introduced by iOS 13 • Extremely easy to build custom complex collection view layouts • Powerful features to beat complex layouts for modern apps

Slide 5

Slide 5 text

https://developer.apple.com/videos/play/wwdc2019/215/ WWDC 2019

Slide 6

Slide 6 text

https://developer.apple.com/videos/play/wwdc2019/215/ WWDC 2019

Slide 7

Slide 7 text

Example: App Store.app

Slide 8

Slide 8 text

Modern App UI is Complicated! • Various rich contents
 must be displayed • Various sizes of content are
 on a scroll view • Nested scrolling

Slide 9

Slide 9 text

How to make it? • Collection View or
 Stack View with Scroll View? • Collection View on Collection View • Collection View Custom Layouts

Slide 10

Slide 10 text

Problem: Building Custom Collection View Layouts

Slide 11

Slide 11 text

How to Build Custom Collection View Layouts • Subclass UICollectionViewLayout • Override methods as needed.

Slide 12

Slide 12 text

The problem Why difficult to build custom collection view layouts

Slide 13

Slide 13 text

Why Difficult to Build Custom Collection View Layouts? • Implement everything yourself • I don't know where to start • Error prone • Too flexible, overkill

Slide 14

Slide 14 text

UICollectionViewLayout class UICollectionViewLayout { class var layoutAttributesClass: AnyClass class var invalidationContextClass: AnyClass func prepare() func layoutAttributesForElements(in rect: CGRect) func layoutAttributesForItem(at indexPath: IndexPath) func layoutAttributesForSupplementaryView(ofKind: String, at: IndexPath) func layoutAttributesForDecorationView(ofKind: String, at: IndexPath) func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) func invalidationContext(forBoundsChange newBounds: CGRect) func shouldInvalidateLayout(forPreferredLayoutAttributes:, withOriginalAttributes:) func invalidationContext(forPreferredLayoutAttributes:, withOriginalAttributes:) func targetContentOffset(forProposedContentOffset: CGPoint, withScrollingVelocity: CGPoint) func targetContentOffset(forProposedContentOffset: CGPoint) var collectionViewContentSize: CGSize ...

Slide 15

Slide 15 text

Easy Simple Complex Collection View on Collection View Custom Layouts Hard Flow Layout Layout Layout

Slide 16

Slide 16 text

Easy Simple Complex Collection View on Collection View Custom Layouts Hard Flow Layout Implementation Layout Layout

Slide 17

Slide 17 text

Easy Simple Complex Collection View on Collection View Custom Layouts Hard Flow Layout Compositional Layouts Implementation Layout Layout

Slide 18

Slide 18 text

Anatomy: Compositional Layouts

Slide 19

Slide 19 text

App Store.app

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

Layout, Section, Group, Item Anatomy: Compositional Layouts

Slide 22

Slide 22 text

Layout, Section, Group, Item Anatomy: Compositional Layouts

Slide 23

Slide 23 text

Layout, Section, Group, Item Anatomy: Compositional Layouts

Slide 24

Slide 24 text

Layout, Section, Group, Item Anatomy: Compositional Layouts

Slide 25

Slide 25 text

Layout, Section, Group, Item Anatomy: Compositional Layouts

Slide 26

Slide 26 text

Layout, Section, Group, Item Layout

Slide 27

Slide 27 text

Layout, Section, Group, Item Layout Section

Slide 28

Slide 28 text

Layout, Section, Group, Item Section Layout

Slide 29

Slide 29 text

Layout, Section, Group, Item Section Layout

Slide 30

Slide 30 text

Layout, Section, Group, Item Layout Section Group

Slide 31

Slide 31 text

Layout, Section, Group, Item Layout Section Group

Slide 32

Slide 32 text

Layout, Section, Group, Item Layout Section Group

Slide 33

Slide 33 text

Layout, Section, Group, Item Layout Section Group

Slide 34

Slide 34 text

Layout, Section, Group, Item Layout Section Group

Slide 35

Slide 35 text

Layout, Section, Group, Item Layout Section Group

Slide 36

Slide 36 text

Layout, Section, Group, Item Layout Section Group Item

Slide 37

Slide 37 text

Layout, Section, Group, Item Layout Section Group Item

Slide 38

Slide 38 text

Layout, Section, Group, Item Layout Section Group Item

Slide 39

Slide 39 text

Layout, Section, Group, Item Layout Section Group Item

Slide 40

Slide 40 text

Layout, Section, Group, Item Layout Section Group Item

Slide 41

Slide 41 text

Layout, Section, Group, Item Layout Section Group Item

Slide 42

Slide 42 text

Section

Slide 43

Slide 43 text

Group

Slide 44

Slide 44 text

Group Item

Slide 45

Slide 45 text

let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1)) let item = NSCollectionLayoutItem(layoutSize: itemSize) let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(50)) let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item]) let section = NSCollectionLayoutSection(group: group)

Slide 46

Slide 46 text

let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1)) let item = NSCollectionLayoutItem(layoutSize: itemSize) let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(50)) let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item]) let section = NSCollectionLayoutSection(group: group)

Slide 47

Slide 47 text

let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1)) let item = NSCollectionLayoutItem(layoutSize: itemSize) let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(50)) let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item]) let section = NSCollectionLayoutSection(group: group)

Slide 48

Slide 48 text

let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1)) let item = NSCollectionLayoutItem(layoutSize: itemSize) let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(50)) let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item]) let section = NSCollectionLayoutSection(group: group)

Slide 49

Slide 49 text

let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1)) let item = NSCollectionLayoutItem(layoutSize: itemSize) let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(50)) let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item]) let section = NSCollectionLayoutSection(group: group)

Slide 50

Slide 50 text

let itemSize = ...Size(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1)) 100% 100%

Slide 51

Slide 51 text

let itemSize = ...Size(widthDimension: .fractionalWidth(0.5), heightDimension: .fractionalHeight(1)) 50% 100% 50% 100%

Slide 52

Slide 52 text

let groupSize = ...Size(widthDimension: .fractionalWidth(1), heightDimension: .absolute(50)) 100% 50pt

Slide 53

Slide 53 text

let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1)) let item = NSCollectionLayoutItem(layoutSize: itemSize) let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .absolute(50)) let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item]) let section = NSCollectionLayoutSection(group: group)

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1)) let item = NSCollectionLayoutItem(layoutSize: itemSize) let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.92), heightDimension: .fractionalHeight(0.4)) let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item]) let section = NSCollectionLayoutSection(group: group)

Slide 56

Slide 56 text

let groupSize = ...Size(widthDimension: .fractionalWidth(0.92), heightDimension: .fractionalHeight(0.4)) 92% 40%

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), heightDimension: .fractionalHeight(1)) let item = NSCollectionLayoutItem(layoutSize: itemSize) let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.92), heightDimension: .fractionalHeight(0.3)) let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitem: item, count: 3) let section = NSCollectionLayoutSection(group: group)

Slide 59

Slide 59 text

let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitem: item, count: 3)

Slide 60

Slide 60 text

let layout = UICollectionViewCompositionalLayout { (sectionIndex, environment) -> NSCollectionLayoutSection? in switch sectionIndex { case 0: ... return section case 1: ... return section ... } } Compose Them into Layout

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

No content

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

No content

Slide 66

Slide 66 text

No content

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

No content

Slide 70

Slide 70 text

No content

Slide 71

Slide 71 text

... let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.92), heightDimension: .fractionalHeight(0.4)) let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitems: [itemGroup]) let section = NSCollectionLayoutSection(group: group) section.orthogonalScrollingBehavior = .groupPagingCentered

Slide 72

Slide 72 text

Orthogonal Scrolling Behavior ... let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(0.92), heightDimension: .fractionalHeight(0.4)) let group = NSCollectionLayoutGroup.vertical(layoutSize: groupSize, subitems: [itemGroup]) let section = NSCollectionLayoutSection(group: group) section.orthogonalScrollingBehavior = .groupPagingCentered

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

https://github.com/kishikawakatsumi/AppStore-Clone-CollectionViewCompositionalLayouts

Slide 79

Slide 79 text

Only iOS 13?

Slide 80

Slide 80 text

https://github.com/kishikawakatsumi/IBPCollectionViewCompositionalLayout

Slide 81

Slide 81 text

IBPCollectionViewCompositionalLayout

Slide 82

Slide 82 text

Under development ✅ Spacing ✅ Nested Groups ✅ Supplemental Views (e.g. Section Header/Footers) ✅ Pinned Section Header/Footers ✅ Orthogonal Scrolling Behavior ✅ Estimated Size (Autosizing) ✅ Custom Group Item (Absolute Positions) ✅ Drop-in replacement ❌ RTL Support

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

Drop-in Replacement - (instancetype)initWithSection:(IBPNSCollectionLayoutSection *)section { if (@available(iOS 13, *)) { return [[NSClassFromString(@"UICollectionViewCompositionalLayout") alloc] initWithSection:section]; } else { IBPUICollectionViewCompositionalLayoutConfiguration *configuration = [IBPUICollectionViewCompositionalLayoutConfiguration defaultConfiguration]; return [self initWithSection:section configuration:configuration]; } }

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

UICollectionViewLayout class UICollectionViewLayout { ... func prepare() func layoutAttributesForElements(in rect: CGRect) -> [UICollecti func layoutAttributesForItem(at indexPath: IndexPath) -> UIColle func layoutAttributesForSupplementaryView(ofKind elementKind: St func layoutAttributesForDecorationView(ofKind elementKind: Strin func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) - ... var collectionViewContentSize: CGSize { get } ...

Slide 87

Slide 87 text

UICollectionViewLayout class UICollectionViewLayout { ... func prepare() func layoutAttributesForElements(in rect: CGRect) -> [UICollecti func layoutAttributesForItem(at indexPath: IndexPath) -> UIColle func layoutAttributesForSupplementaryView(ofKind elementKind: St func layoutAttributesForDecorationView(ofKind elementKind: Strin func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) - ... var collectionViewContentSize: CGSize { get } ...

Slide 88

Slide 88 text

UICollectionViewLayout class UICollectionViewLayout { ... func prepare() func layoutAttributesForElements(in rect: CGRect) -> [UICollecti func layoutAttributesForItem(at indexPath: IndexPath) -> UIColle func layoutAttributesForSupplementaryView(ofKind elementKind: St func layoutAttributesForDecorationView(ofKind elementKind: Strin func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) - ... var collectionViewContentSize: CGSize { get } ...

Slide 89

Slide 89 text

UICollectionViewLayout class UICollectionViewLayout { ... func prepare() func layoutAttributesForElements(in rect: CGRect) -> [UICollecti func layoutAttributesForItem(at indexPath: IndexPath) -> UIColle func layoutAttributesForSupplementaryView(ofKind elementKind: St func layoutAttributesForDecorationView(ofKind elementKind: Strin func shouldInvalidateLayout(forBoundsChange newBounds: CGRect) - ... var collectionViewContentSize: CGSize { get } ...

Slide 90

Slide 90 text

No content

Slide 91

Slide 91 text

No content

Slide 92

Slide 92 text

Wrap up • Collection View Compositional Layouts is incredible! • No need to implement collection view layout subclass • Like just editing configurations • Keep your code simple regardless layout become complex • You can use it now! (without iOS 13)

Slide 93

Slide 93 text

Pitfalls

Slide 94

Slide 94 text

No Documentation

Slide 95

Slide 95 text

2019-09-20 07:14:13.535020+0900 ... [29591:20271214] [CompositionalLayout] Attempting to add contentInsets to an item's dimension along an estimated axis. This layout axis insets will be ignored (however, any non-estimated axis insets will be applied). Please consider using the item's edgeSpacing or the containing group's interItemSpacing instead.

Slide 96

Slide 96 text

References • IBPCollectionViewCompositionalLayout
 https://github.com/kishikawakatsumi/IBPCollectionViewCompositionalLayout • App Store.app Clone
 https://github.com/kishikawakatsumi/AppStore-Clone-CollectionViewCompositionalLayouts

Slide 97

Slide 97 text

References • Advances in Collection View Layout
 https://developer.apple.com/videos/play/wwdc2019/215/ • All you need to know about UICollectionViewCompositionalLayout
 https://medium.com/flawless-app-stories/all-what-you-need-to-know-about- uicollectionviewcompositionallayout-f3b2f590bdbe • Using CollectionView Compositional Layouts in Swift 5
 https://dev.to/kevinmaarek/using-collectionview-compositional-layouts-in-swift-5-1nan • Move your cells left to right, up and down on iOS 13 — Part 1
 https://medium.com/shopback-engineering/move-your-cells-left-to-right-up-and-down-on- ios-13-part-1-1a5e010f48f9