By default, when you are using the flow layout in a collection view, cells are centered vertically. Is there a way to change this alignment ?
相关问题
- 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
- Get the NSRange for the visible text after scroll
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- Xcode: Is there a way to change line spacing (UI L
- Swift - hide pickerView after value selected
- How do you detect key up / key down events from a
- didBeginContact:(SKPhysicsContact *)contact not in
@DongXu: Your solution worked for me too. Here is the SWIFT version if it:
This may or may not work for your particular situation, but I had some luck subclassing
UICollectionViewFlowLayout
in the following way:I used this code (https://github.com/yoeriboven/TopAlignedCollectionViewLayout) after DongXu's solution didn't quite work. The only modification was that it's originally designed to be used with a grid so I needed to instantiate the layout with an arbitrarily high column count...
Swift 4 with functional oriented approach:
Swift 3 Version in case someone just wants to Copy & Paste:
The
UICollectionViewFlowLayout
class is derived from theUICollectionViewLayout
base class. And if you look at the documentation for that, you'll see there are a number of methods you can override, the most likely candidate beinglayoutAttributesForItemAtIndexPath:
.If you override that method, you could let it call its super implementation, and then adjust the properties of the returned
UICollectionViewLayoutAttributes
object. Specifically, you'll likely need to adjust theframe
property to reposition the item so it's no longer centered.