Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Custom Collection View Layouts
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Pawel Dudek
July 26, 2014
Programming
93
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Custom Collection View Layouts
Pawel Dudek
July 26, 2014
More Decks by Pawel Dudek
See All by Pawel Dudek
Swift Mocks
paweldudek
0
230
BDD DEVit
paweldudek
0
420
BDD NL
paweldudek
0
270
BDD Bialystok/Wroclaw
paweldudek
0
190
Poznan TDD Workshop Part 1
paweldudek
0
86
TDD Workshop - UIKonf 2016
paweldudek
0
270
Behaviour Driven Development - Mobile Trends
paweldudek
1
380
BDD Mobilization
paweldudek
0
130
Behaviour Driven Development - the whys and hows
paweldudek
0
190
Other Decks in Programming
See All in Programming
20260623_Loop Engineeringで自分の分身の問い合わせBotを作る
ryugen04
0
130
Webフレームワークの ベンチマークについて
yusukebe
0
200
【やさしく解説 設計編・中級 #1】一つの車に、運転手は一人 ~ある倉庫システムの事例から~
panda728
PRO
0
110
キャリア迷子上等 ─ "ない道"は自分で作ればいい
16bitidol
3
2.8k
決定論的オーケストレーションの設計と実装 / Design and Implementation of Deterministic Orchestration
nrslib
4
1.6k
ローカルLLMでどこまでコードが書けるか -拡張版 / How much code can be written on a local LLM Extended
kishida
12
4.6k
OS アップデート対応の取り組み方がもっと共有されてほしい
andpad
0
110
SREは、MCPとSRE Agentをこう使え!
kazumax55
0
140
AI時代の仕事技芸論〜ソフトウェア開発で「遊ぶように働く」職人的熟達のすすめ(スクフェス仙台 2026バージョン)
kuranuki
0
390
Even G2とAWSで推しのエージェントを召喚しよう!
har1101
1
140
IBM Bobを活用したレガシーアプリの最新化
oniak3ibm
PRO
1
240
Skillsは効率化、Agentsは"自分の拡張"——Builder時代のエージェント編成(CC Night 2026)
wemra
1
200
Featured
See All Featured
Automating Front-end Workflow
addyosmani
1370
210k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.7k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
370
GraphQLとの向き合い方2022年版
quramy
50
15k
技術選定の審美眼(2025年版) / Understanding the Spiral of Technologies 2025 edition
twada
PRO
118
120k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
333
23k
jQuery: Nuts, Bolts and Bling
dougneiner
66
8.5k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Facilitating Awesome Meetings
lara
57
7k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
1
1.9k
Abbi's Birthday
coloredviolet
3
8.5k
HTML-Aware ERB: The Path to Reactive Rendering @ RubyCon 2026, Rimini, Italy
marcoroth
2
300
Transcript
CUSTOM COLLECTION VIEW LAYOUTS
WHO HAS EVER BUILT A CUSTOM LAYOUT?
THAT DOES NOT INHERIT FROM FLOW LAYOUT?
CUSTOM LAYOUT VS INHERITING FROM FLOW LAYOUT
WHENEVER YOU CAN USE FLOW LAYOUT Flow layout is a
line breaking layout. Any variation that lays items in a line and breaks when it gets to an end can be covered with flow layout.
WHENEVER YOU CAN USE FLOW LAYOUT ▸ Add new views
▸ Tweak layout attributes ▸ New layout attributes ▸ Gesture support ▸ Custom insert/delete animations
FLOW LAYOUT IS HIGHLY OPTIMIZED AND VERSATILE TOOL
YOU SHOULD USE IT
WHY WOULD I WANT TO BUILD A CUSTOM LAYOUT?
WHAT ARE THE TYPES OF LAYOUTS?
LAYOUT DRIVEN
LAYOUT DRIVEN THINK FLOW LAYOUT
DATA DRIVEN
DATA DRIVEN THINK CALENDAR LAYOUT
UICOLLECTIONVIEWLAYOUT ATTRIBUTES
DEFINES HOW ITEMS ARE LAID OUT BY YOUR COLLECTION VIEW.
NS_CLASS_AVAILABLE_IOS(6_0) @interface UICollectionViewLayoutAttributes : NSObject @property (nonatomic) CGRect frame; @property
(nonatomic) CGPoint center; @property (nonatomic) CGSize size; @property (nonatomic) CATransform3D transform3D; @property (nonatomic) CGRect bounds NS_AVAILABLE_IOS(7_0); @property (nonatomic) CGAffineTransform transform NS_AVAILABLE_IOS(7_0); @property (nonatomic) CGFloat alpha; @property (nonatomic) NSInteger zIndex; // default is 0 @property (nonatomic, getter=isHidden) BOOL hidden; @property (nonatomic, retain) NSIndexPath *indexPath; @property (nonatomic, readonly) UICollectionElementCategory representedElementCategory; @property (nonatomic, readonly) NSString *representedElementKind;
YOU CAN PROVIDE YOUR OWN SUBCLASS TO PASS ADDITIONAL INFORMATION
TO CELLS, SUPPLEMENTARY AND DECORATION VIEWS.
NS_CLASS_AVAILABLE_IOS(6_0) @interface UICollectionReusableView : UIView (…) - (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)attributes; (…)
@end
YOU HAVE TO OVERRIDE isEqual & copy
BUILDING CUSTOM COLLECTION VIEW LAYOUT
HOW DOES UICOLLECTIONVIEW PREPARE ITS LAYOUT? - (void)prepareLayout; - (CGSize)collectionViewContentSize;
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect;
PREPARE LAYOUT - (void)prepareLayout { [super prepareLayout]; (...) }
CONTENT SIZE - (CGSize)collectionViewContentSize { CGSize myCustomSize = (...) return
myCustomSize; }
LAYOUT ATTRIBUTES FOR ELEMENTS IN RECT - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
return [super layoutAttributesForElementsInRect:rect]; }
The rect parameter does not define whole bounds of collection
view.
UICOLLECTIONVIEW CONSIDERS ITS LAYOUT UP AND READY AFTER THESE THREE
METHODS ARE CALLED.
LAYOUT ATTRIBUTES FOR ELEMENT AT INDEX PATH - (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath
*)path { return [super layoutAttributesForItemAtIndexPath:indexPath]; }
INVALIDATING LAYOUT WHEN?
INVALIDATE WHEN A VALUE THAT DRIVES LAYOUT CHANGES ▸ Bounds
▸ Content offset ▸ Data (data driven layouts) ▸ Layout-specific
INVALIDATING LAYOUT [layout invalidateLayout];
INVALIDATING LAYOUT - (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds { return [super shouldInvalidateLayoutForBoundsChange:newBounds]; }
ASSIGNMENT 1 CAROUSEL LAYOUT
GOALS ▸ Behaves like paging in scroll view with horizontal
scroling ▸ Item is always centered veritcally when scroll rests ▸ When iPad is rotated the same cell stays in the middle of the screen ▸ No code related to setting up layout in controller ▸ Does not inherit from UICollectionViewFlowLayout (optional)
EXAMPLE
None
TIPS
USE TARGET CONTENT OFFSET WITH VELOCITY - (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset withScrollingVelocity:(CGPoint)velocity;
AND TARGET CONTENT OFFSET - (CGPoint)targetContentOffsetForProposedContentOffset:(CGPoint)proposedContentOffset;
None
LET'S CODE!
ASSIGNMENT 2 CALENDAR LAYOUT LET'S TRY SOMETHING HARDER
GOALS ▸ Displays items of different sizes that represent events
▸ Displays a 'bead' view that shows current time ▸ Does not inherit from flow layout
EXAMPLE
None
GETTING DATA TO YOUR COLLECTION VIEW LAYOUT
THE EXTENSIBLE DELEGATE PATTERN
@protocol CalendarCollectionViewLayoutDelegate <UICollectionViewDelegate>
WHAT DATA WILL YOU NEED?
TIME TO POSITION BEAD VIEW
EVENTS FOR CALENDAR
START OF DISPLAYED DAY
END OF DISPLAYED DATE
BEAD VIEW CONSIDER IT AS A DECORATION VIEW
REGISTERING A DECORATION VIEW [self registerClass:[BeadView class] forDecorationViewOfKind:DecorationKindBead];
PROVIDING DECORATION LAYOUT ATTRIBUTES SAME AS WITH CELL LAYOUT ATTRIBUTES
- (UICollectionViewLayoutAttributes *)layoutAttributesForDecorationViewOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
[UICollectionViewLayoutAttributes layoutAttributesForDecorationViewOfKind:DecorationKindBead withIndexPath:indexPath];
HAVE TO ALSO BE PASSED IN LAYOUT ATTRIBUTES FOR RECT
1 MINUTE EQUALS 1 PIXEL
[self.startOfDisplayedDay mt_minutesUntilDate:self.endOfDisplayedDay];
None
LET'S CODE!
ASSIGNMENT 3 CALENDAR LAYOUT SEPARATORS
GOALS ▸ Display dark gray line at each hour ▸
Display light gray line at each half an hour ▸ Display hour next to each dark gray line
EXAMPLE
None
TIPS
USE CUSTOM LAYOUT ATTRIBUTES TO GET COLOR/TEXT TO SEPARATOR VIEW
SEPARATORS DECORATION VIEWS
LET'S CODE!
CONCLUSION ▸ Flow layout can be easily modified to developer
needs ▸ Building custom layouts is not that hard ▸ Layout attributes can be a great way of providing additional data to collection view elements
THANK YOU FOR YOUR ATTENTION!
PAWEL DUDEK @ELDUDI HTTPS://GITHUB.COM/PAWELDUDEK