I wanna making 2pt black separators in UICollectionView
for our new app. Screenshot from our app is below. We couldn't use UITableView
, because we have custom insert/delete animations, scrolling and parallax effects and so on.
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
- How can I add media attachments to my push notific
I started with three ideas how to make it:
minimumLineSpacing
, thus we will see background in spaces between cellsFirst two variants were rejected because ideologic inconsistency, custom animations and having content below collection. Also I already have a custom layout.
I will describe the steps with a custom subclass of
UICollectionViewFlowLayout
.--1--
Implement custom
UICollectionReusableView
subclass.--2--
Say layout to use custom decorations. Also make line spacing between cells.
--3--
In custom
UICollectionViewFlowLayout
subclass we should returnUICollectionViewLayoutAttributes
for decorations fromlayoutAttributesForElementsInRect
.--4--
For visible rect we should return visible decorations index pathes.
--5--
Also we should implement
layoutAttributesForDecorationViewOfKind
.--6--
Sometimes I found that this solution gives visual glitches with decorations appearance, which was fixed with implementing
initialLayoutAttributesForAppearingDecorationElementOfKind
.That's all. Not too much code but done right.
Excellent suggestion by Anton, but I think the implementation in the FlowLayout sub class can be even simpler. Because the super implementation of - (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect already returns the layout attributes of the cells including their frame and indexPath you have enough info to calculate the frames of the separators by overriding only this method and introspecting the cell layout attributes:
Here is the version from Anton Gaenko but implemented in C#, this could be usefull for Xamarin users :
Thanks, Anton & Werner, both helped me out - I've taken your help to make a drag & drop solution, as a category on
UICollectionView
, thought I'd share the results:UICollectionView+Separators.h
UICollectionView+Separators.m
Using Objective-C runtime and some swizzling, cell separators can be added with a couple of lines to any existing
UICollectionView
whose layout is/inherits fromUICollectionViewFlowLayout
.Example Usage:
A couple notes:
collectionView:layout:minimumLineSpacingForSectionAtIndex:
, falling back onminimumLineSpacing
if not implementedHope it helps
Quick solution in Swift
1. Create CustomFlowLayout.swift file and paste next code
2. Setup custom flow
In the interface builder select your UICollectionViewFlow and set our new class name
CustomFlowLayout
3. Change a separator color
In SeparatorView you can change the color of separator in
init
4. Change a height of the separator
You can do it in two different ways
Min Spacing for Lines
OR
In the code. Set value for
minimumLineSpacing