ios 你如何确定 UICollectionView flowLayout 中单元格之间的间距
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13017257/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you determine spacing between cells in UICollectionView flowLayout
提问by adit
I have a UICollectionView with a flow layout and each cell is a square. How do I determine the spacing between each cells on each row? I can't seem to find the appropriate settings for this. I see there's a min spacing attributes on the nib file for a collection view, but I set this to 0 and the cells doesn't even stick.
我有一个带有流布局的 UICollectionView,每个单元格都是一个正方形。如何确定每行每个单元格之间的间距?我似乎无法为此找到合适的设置。我看到 nib 文件上有一个用于集合视图的最小间距属性,但我将其设置为 0 并且单元格甚至不粘连。
Any other idea?
还有其他想法吗?
回答by Chris Wagner
Update: Swift version of this answer: https://github.com/fanpyi/UICollectionViewLeftAlignedLayout-Swift
更新:此答案的 Swift 版本:https: //github.com/fanpyi/UICollectionViewLeftAlignedLayout-Swift
Taking @matt's leadI modified his code to insure that items are ALWAYS left aligned. I found that if an item ended up on a line by itself, it would be centered by the flow layout. I made the following changes to address this issue.
在@matt 的带领下,我修改了他的代码以确保项目始终左对齐。我发现如果一个项目自己结束在一行上,它会以流布局为中心。我进行了以下更改以解决此问题。
This situation would only ever occur if you have cells that vary in width, which could result in a layout like the following. The last line always left aligns due to the behavior of UICollectionViewFlowLayout
, the issue lies in items that are by themselves in any line but the last one.
只有当单元格宽度不同时才会发生这种情况,这可能会导致如下布局。由于 的行为,最后一行总是左对齐UICollectionViewFlowLayout
,问题在于除最后一行之外的任何一行中的项目。
With @matt's code I was seeing.
使用@matt 的代码,我看到了。
In that example we see that cells get centered if they end up on the line by themselves. The code below insures your collection view would look like this.
在那个例子中,我们看到如果单元格自己最终在行上,它们就会居中。下面的代码确保您的集合视图看起来像这样。
#import "CWDLeftAlignedCollectionViewFlowLayout.h"
const NSInteger kMaxCellSpacing = 9;
@implementation CWDLeftAlignedCollectionViewFlowLayout
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray* attributesToReturn = [super layoutAttributesForElementsInRect:rect];
for (UICollectionViewLayoutAttributes* attributes in attributesToReturn) {
if (nil == attributes.representedElementKind) {
NSIndexPath* indexPath = attributes.indexPath;
attributes.frame = [self layoutAttributesForItemAtIndexPath:indexPath].frame;
}
}
return attributesToReturn;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes* currentItemAttributes =
[super layoutAttributesForItemAtIndexPath:indexPath];
UIEdgeInsets sectionInset = [(UICollectionViewFlowLayout *)self.collectionView.collectionViewLayout sectionInset];
CGRect currentFrame = currentItemAttributes.frame;
if (indexPath.item == 0) { // first item of section
currentFrame.origin.x = sectionInset.left; // first item of the section should always be left aligned
currentItemAttributes.frame = currentFrame;
return currentItemAttributes;
}
NSIndexPath* previousIndexPath = [NSIndexPath indexPathForItem:indexPath.item-1 inSection:indexPath.section];
CGRect previousFrame = [self layoutAttributesForItemAtIndexPath:previousIndexPath].frame;
CGFloat previousFrameRightPoint = CGRectGetMaxX(previousFrame) + kMaxCellSpacing;
CGRect strecthedCurrentFrame = CGRectMake(0,
currentFrame.origin.y,
self.collectionView.frame.size.width,
currentFrame.size.height);
if (!CGRectIntersectsRect(previousFrame, strecthedCurrentFrame)) { // if current item is the first item on the line
// the approach here is to take the current frame, left align it to the edge of the view
// then stretch it the width of the collection view, if it intersects with the previous frame then that means it
// is on the same line, otherwise it is on it's own new line
currentFrame.origin.x = sectionInset.left; // first item on the line should always be left aligned
currentItemAttributes.frame = currentFrame;
return currentItemAttributes;
}
currentFrame.origin.x = previousFrameRightPoint;
currentItemAttributes.frame = currentFrame;
return currentItemAttributes;
}
@end
回答by matt
To get a maximum interitem spacing, subclass UICollectionViewFlowLayout and override layoutAttributesForElementsInRect:
and layoutAttributesForItemAtIndexPath:
.
要获得最大的项目间间距,请继承 UICollectionViewFlowLayout 并覆盖layoutAttributesForElementsInRect:
and layoutAttributesForItemAtIndexPath:
。
For example, a common problem is this: the rows of a collection view are right-and-left justified, except for the last line which is left-justified. Let's say we want allthe lines to be left-justified, so that the space between them is, let's say, 10 points. Here's an easy way (in your UICollectionViewFlowLayout subclass):
例如,一个常见的问题是:集合视图的行是左右对齐的,除了最后一行是左对齐的。假设我们希望所有的线都左对齐,这样它们之间的空间就是 10 点。这是一个简单的方法(在您的 UICollectionViewFlowLayout 子类中):
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect {
NSArray* arr = [super layoutAttributesForElementsInRect:rect];
for (UICollectionViewLayoutAttributes* atts in arr) {
if (nil == atts.representedElementKind) {
NSIndexPath* ip = atts.indexPath;
atts.frame = [self layoutAttributesForItemAtIndexPath:ip].frame;
}
}
return arr;
}
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewLayoutAttributes* atts =
[super layoutAttributesForItemAtIndexPath:indexPath];
if (indexPath.item == 0) // degenerate case 1, first item of section
return atts;
NSIndexPath* ipPrev =
[NSIndexPath indexPathForItem:indexPath.item-1 inSection:indexPath.section];
CGRect fPrev = [self layoutAttributesForItemAtIndexPath:ipPrev].frame;
CGFloat rightPrev = fPrev.origin.x + fPrev.size.width + 10;
if (atts.frame.origin.x <= rightPrev) // degenerate case 2, first item of line
return atts;
CGRect f = atts.frame;
f.origin.x = rightPrev;
atts.frame = f;
return atts;
}
The reason this is so easy is that we aren't really performing the heavy lifting of the layout; we are leveraging the layout work that UICollectionViewFlowLayout has already done for us. It has already decided how many items go in each line; we're just reading those lines and shoving the items together, if you see what I mean.
这如此简单的原因是我们并没有真正执行布局的繁重工作;我们正在利用 UICollectionViewFlowLayout 已经为我们完成的布局工作。它已经决定了每行有多少项目;如果你明白我的意思,我们只是在阅读这些行并将这些项目放在一起。
回答by Caleb
There are a few things to consider:
有几件事情需要考虑:
Try changing the minimum spacing in IB, but leave the cursor in that field. Notice that Xcode doesn't immediately mark the document as changed.When you click in a different field, though, Xcode does notice that the document is changed and marks it so in the file navigator. So, be sure to tab or click over to a different field after making a change.
Save your storyboard/xib file after making a change, and be sure to rebuild the app. It's not hard to miss that step, and then you're left scratching your head wondering why your changes didn't seem to have any effect.
UICollectionViewFlowLayout
has aminimumInteritemSpacing
property, which is what you're setting in IB. But the collection's delegate can also have a method to determine the inter-item spacing. That method trump's the layout's property, so if you implement it in your delegate your layout's property won't be used.Remember that the spacing there is a minimumspacing. The layout will use that number (whether it comes from the property or from the delegate method) as the smallest allowable space, but it may use a larger space if it has space leftover on the line. So if, for example, you set the minimum spacing to 0, you may still see a few pixels between items. If you want more control over exactly how the items are spaced you should probably use a different layout (possibly one of your own creation).
尝试更改 IB 中的最小间距,但将光标留在该字段中。请注意,Xcode 不会立即将文档标记为已更改。但是,当您单击不同的字段时,Xcode 会注意到文档已更改并在文件导航器中将其标记为更改。因此,请务必在进行更改后使用 Tab 键或单击切换到不同的字段。
进行更改后保存您的故事板/xib 文件,并确保重建应用程序。错过这一步并不难,然后你就会挠头想知道为什么你的更改似乎没有任何效果。
UICollectionViewFlowLayout
有一个minimumInteritemSpacing
属性,这就是您在 IB 中设置的属性。但是集合的委托也可以有一个方法来确定项目间的间距。该方法胜过布局的属性,因此如果您在委托中实现它,则不会使用布局的属性。请记住,间距有一个最小间距。布局将使用该数字(无论它来自属性还是来自委托方法)作为最小的允许空间,但如果行上有剩余空间,它可能会使用更大的空间。因此,例如,如果您将最小间距设置为 0,您可能仍会在项目之间看到几个像素。如果您想更好地控制项目的确切间距,您可能应该使用不同的布局(可能是您自己创建的布局之一)。
回答by ldesroziers
A little bit of maths does the trick more easily. The code wrote by Chris Wagner is horrible because it calls the layout attributes of each previous items. So the more you scroll, the more it's slow...
一点数学知识可以更轻松地解决问题。Chris Wagner 编写的代码很糟糕,因为它调用了之前每个项目的布局属性。所以你滚动得越多,它就越慢......
Just use modulo like this (I'm using my minimumInteritemSpacing value as a max value too):
只需像这样使用模(我也使用我的 minimumInteritemSpacing 值作为最大值):
- (UICollectionViewLayoutAttributes *)layoutAttributesForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewLayoutAttributes* currentItemAttributes = [super layoutAttributesForItemAtIndexPath:indexPath];
NSInteger numberOfItemsPerLine = floor([self collectionViewContentSize].width / [self itemSize].width);
if (indexPath.item % numberOfItemsPerLine != 0)
{
NSInteger cellIndexInLine = (indexPath.item % numberOfItemsPerLine);
CGRect itemFrame = [currentItemAttributes frame];
itemFrame.origin.x = ([self itemSize].width * cellIndexInLine) + ([self minimumInteritemSpacing] * cellIndexInLine);
currentItemAttributes.frame = itemFrame;
}
return currentItemAttributes;
}
回答by xytor
An easy way to left-justify is to modify layoutAttributesForElementsInRect: in your subclass of UICollectionViewFlowLayout:
一种左对齐的简单方法是在 UICollectionViewFlowLayout 的子类中修改 layoutAttributesForElementsInRect: :
- (NSArray *)layoutAttributesForElementsInRect:(CGRect)rect
{
NSArray *allLayoutAttributes = [super layoutAttributesForElementsInRect:rect];
CGRect prevFrame = CGRectMake(-FLT_MAX, -FLT_MAX, 0, 0);
for (UICollectionViewLayoutAttributes *layoutAttributes in allLayoutAttributes)
{
//fix blur
CGRect theFrame = CGRectIntegral(layoutAttributes.frame);
//left justify
if(prevFrame.origin.x > -FLT_MAX &&
prevFrame.origin.y >= theFrame.origin.y &&
prevFrame.origin.y <= theFrame.origin.y) //workaround for float == warning
{
theFrame.origin.x = prevFrame.origin.x +
prevFrame.size.width +
EXACT_SPACE_BETWEEN_ITEMS;
}
prevFrame = theFrame;
layoutAttributes.frame = theFrame;
}
return allLayoutAttributes;
}
回答by zirinisp
The swift version of Chris solution.
Chris 解决方案的快速版本。
class PazLeftAlignedCollectionViewFlowLayout : UICollectionViewFlowLayout {
var maxCellSpacing = 14.0
override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {
if var attributesToReturn = super.layoutAttributesForElementsInRect(rect) as? Array<UICollectionViewLayoutAttributes> {
for attributes in attributesToReturn {
if attributes.representedElementKind == nil {
let indexPath = attributes.indexPath
attributes.frame = self.layoutAttributesForItemAtIndexPath(indexPath).frame;
}
}
return attributesToReturn;
}
return super.layoutAttributesForElementsInRect(rect)
}
override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes! {
let currentItemAttributes = super.layoutAttributesForItemAtIndexPath(indexPath)
if let collectionViewFlowLayout = self.collectionView?.collectionViewLayout as? UICollectionViewFlowLayout {
let sectionInset = collectionViewFlowLayout.sectionInset
if (indexPath.item == 0) { // first item of section
var frame = currentItemAttributes.frame;
frame.origin.x = sectionInset.left; // first item of the section should always be left aligned
currentItemAttributes.frame = frame;
return currentItemAttributes;
}
let previousIndexPath = NSIndexPath(forItem:indexPath.item-1, inSection:indexPath.section)
let previousFrame = self.layoutAttributesForItemAtIndexPath(previousIndexPath).frame;
let previousFrameRightPoint = Double(previousFrame.origin.x) + Double(previousFrame.size.width) + self.maxCellSpacing
let currentFrame = currentItemAttributes.frame
var width : CGFloat = 0.0
if let collectionViewWidth = self.collectionView?.frame.size.width {
width = collectionViewWidth
}
let strecthedCurrentFrame = CGRectMake(0,
currentFrame.origin.y,
width,
currentFrame.size.height);
if (!CGRectIntersectsRect(previousFrame, strecthedCurrentFrame)) { // if current item is the first item on the line
// the approach here is to take the current frame, left align it to the edge of the view
// then stretch it the width of the collection view, if it intersects with the previous frame then that means it
// is on the same line, otherwise it is on it's own new line
var frame = currentItemAttributes.frame;
frame.origin.x = sectionInset.left; // first item on the line should always be left aligned
currentItemAttributes.frame = frame;
return currentItemAttributes;
}
var frame = currentItemAttributes.frame;
frame.origin.x = CGFloat(previousFrameRightPoint)
currentItemAttributes.frame = frame;
}
return currentItemAttributes;
}
}
To use it do the following:
要使用它,请执行以下操作:
override func viewDidLoad() {
super.viewDidLoad()
self.collectionView.collectionViewLayout = self.layout
}
var layout : PazLeftAlignedCollectionViewFlowLayout {
var layout = PazLeftAlignedCollectionViewFlowLayout()
layout.itemSize = CGSizeMake(220.0, 230.0)
layout.minimumLineSpacing = 12.0
return layout
}
回答by Mischa
The "problem" with UICollectionViewFlowLayout
is that it applies a justified align to the cells: The first cell in a row is left-aligned, the last cell in a row is right-aligned and all other cells in between are evenly distributed with an equal spacing that's greater than the minimumInteritemSpacing
.
“问题”UICollectionViewFlowLayout
在于它对单元格应用了对齐对齐:一行中的第一个单元格左对齐,一行中的最后一个单元格右对齐,中间的所有其他单元格均等间距均匀分布这比minimumInteritemSpacing
.
There are already many great answers to this post that solve this problem by subclassing UICollectionViewFlowLayout
. As a result you get a layout that aligns the cells left. Another valid solution to distribute the cells with a constant spacing is to align the cells right.
这篇文章已经有很多很好的答案,可以通过子类化来解决这个问题UICollectionViewFlowLayout
。因此,您会得到一个将单元格向左对齐的布局。以恒定间距分布单元格的另一个有效解决方案是将单元格右对齐。
AlignedCollectionViewFlowLayout
AlignedCollectionViewFlowLayout
I've created a UICollectionViewFlowLayout
subclass as well that follows a similar idea as suggested by mattand Chris Wagnerthat can either align the cells
我还创建了一个UICollectionViewFlowLayout
子类,它遵循与matt和Chris Wagner建议的类似想法,可以对齐单元格
??left:
??左:
or ??right:
或??对:
You can simply download it from here, add the layout file to your project and set AlignedCollectionViewFlowLayout
as your collection view's layout class:
https://github.com/mischa-hildebrand/AlignedCollectionViewFlowLayout
你可以简单地从这里下载它,将布局文件添加到你的项目中并设置AlignedCollectionViewFlowLayout
为你的集合视图的布局类:https:
//github.com/mischa-hildebrand/AlignedCollectionViewFlowLayout
How it works (for left-aligned cells):
它是如何工作的(对于左对齐的单元格):
+---------+----------------------------------------------------------------+---------+
| | | |
| | +------------+ | |
| | | | | |
| section |- - -|- - - - - - |- - - - +---------------------+ - - - - - - -| section |
| inset | |intersection| | | line rect | inset |
| |- - -|- - - - - - |- - - - +---------------------+ - - - - - - -| |
| (left) | | | current item | (right) |
| | +------------+ | |
| | previous item | |
+---------+----------------------------------------------------------------+---------+
The concept here is to check if the current cell with index iand the previous cell with the index i?1occupy the same line.
这里的概念是检查索引为i的当前单元格和索引为i?1的前一个单元格是否占据同一行。
- If they don't the cell with index iis the left most cell in the line.
→ Move the cell to the left edge of the collection view (without changing its vertical position). - If they do, the cell with index iis not the left most cell in the line.
→ Get the previous cell's frame (with the index i?1) and move the current cell next to it.
- 如果没有,索引为i的单元格是该行中最左边的单元格。
→ 将单元格移动到集合视图的左边缘(不改变其垂直位置)。 - 如果是,则索引为i的单元格不是该行中最左边的单元格。
→ 获取前一个单元格的帧(索引为i?1)并将当前单元格移动到它旁边。
For right-aligned cells...
对于右对齐的单元格...
... you do the same vice-versa, i.e. you check the nextcell with the index i+1instead.
...你做同样的反之亦然,即你检查下一个带有索引i+1 的单元格。
回答by C?ur
Clean Swift solution, from an history of evolution:
Clean Swift 解决方案,来自进化史:
- there was matt answer
- there was Chris Wagner lone items fix
- there was mokagio sectionInset and minimumInteritemSpacing improvement
- there was fanpyi Swift version
- now here is a simplified and clean version of mine:
- 有马特答案
- 有Chris Wagner 单独的项目修复
- 有mokagio sectionInset 和 minimumInteritemSpacing 改进
- 有fanpyi Swift 版本
- 现在这是我的一个简化和干净的版本:
open class UICollectionViewLeftAlignedLayout: UICollectionViewFlowLayout {
open override func layoutAttributesForElements(in rect: CGRect) -> [UICollectionViewLayoutAttributes]? {
return super.layoutAttributesForElements(in: rect)?.map { class AlignLeftFlowLayout: UICollectionViewFlowLayout {
var maximumCellSpacing = CGFloat(9.0)
override func layoutAttributesForElementsInRect(rect: CGRect) -> [AnyObject]? {
let attributesToReturn = super.layoutAttributesForElementsInRect(rect) as? [UICollectionViewLayoutAttributes]
for attributes in attributesToReturn ?? [] {
if attributes.representedElementKind == nil {
attributes.frame = self.layoutAttributesForItemAtIndexPath(attributes.indexPath).frame
}
}
return attributesToReturn
}
override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> UICollectionViewLayoutAttributes! {
let curAttributes = super.layoutAttributesForItemAtIndexPath(indexPath)
let sectionInset = (self.collectionView?.collectionViewLayout as UICollectionViewFlowLayout).sectionInset
if indexPath.item == 0 {
let f = curAttributes.frame
curAttributes.frame = CGRectMake(sectionInset.left, f.origin.y, f.size.width, f.size.height)
return curAttributes
}
let prevIndexPath = NSIndexPath(forItem: indexPath.item-1, inSection: indexPath.section)
let prevFrame = self.layoutAttributesForItemAtIndexPath(prevIndexPath).frame
let prevFrameRightPoint = prevFrame.origin.x + prevFrame.size.width + maximumCellSpacing
let curFrame = curAttributes.frame
let stretchedCurFrame = CGRectMake(0, curFrame.origin.y, self.collectionView!.frame.size.width, curFrame.size.height)
if CGRectIntersectsRect(prevFrame, stretchedCurFrame) {
curAttributes.frame = CGRectMake(prevFrameRightPoint, curFrame.origin.y, curFrame.size.width, curFrame.size.height)
} else {
curAttributes.frame = CGRectMake(sectionInset.left, curFrame.origin.y, curFrame.size.width, curFrame.size.height)
}
return curAttributes
}
}
.representedElementKind == nil ? layoutAttributesForItem(at: ##代码##.indexPath)! : ##代码## }
}
open override func layoutAttributesForItem(at indexPath: IndexPath) -> UICollectionViewLayoutAttributes? {
guard let currentItemAttributes = super.layoutAttributesForItem(at: indexPath)?.copy() as? UICollectionViewLayoutAttributes,
collectionView != nil else {
// should never happen
return nil
}
// if the current frame, once stretched to the full row intersects the previous frame then they are on the same row
if indexPath.item != 0,
let previousFrame = layoutAttributesForItem(at: IndexPath(item: indexPath.item - 1, section: indexPath.section))?.frame,
currentItemAttributes.frame.intersects(CGRect(x: -.infinity, y: previousFrame.origin.y, width: .infinity, height: previousFrame.size.height)) {
// the next item on a line
currentItemAttributes.frame.origin.x = previousFrame.origin.x + previousFrame.size.width + evaluatedMinimumInteritemSpacingForSection(at: indexPath.section)
} else {
// the first item on a line
currentItemAttributes.frame.origin.x = evaluatedSectionInsetForSection(at: indexPath.section).left
}
return currentItemAttributes
}
func evaluatedMinimumInteritemSpacingForSection(at section: NSInteger) -> CGFloat {
return (collectionView?.delegate as? UICollectionViewDelegateFlowLayout)?.collectionView?(collectionView!, layout: self, minimumInteritemSpacingForSectionAt: section) ?? minimumInteritemSpacing
}
func evaluatedSectionInsetForSection(at index: NSInteger) -> UIEdgeInsets {
return (collectionView?.delegate as? UICollectionViewDelegateFlowLayout)?.collectionView?(collectionView!, layout: self, insetForSectionAt: index) ?? sectionInset
}
}
Usage: the spacing between items is determined by delegate's collectionView (_:layout:minimumInteritemSpacingForSectionAt:)
.
用法:项目之间的间距由委托的collectionView (_:layout:minimumInteritemSpacingForSectionAt:)
.
I put it on github, https://github.com/Coeur/UICollectionViewLeftAlignedLayout, where I actually added a feature of supporting both scroll directions (horizontal and vertical).
我把它放在 github 上,https://github.com/Coeur/UICollectionViewLeftAlignedLayout,我实际上在那里添加了一个支持两个滚动方向(水平和垂直)的功能。
回答by dengApro
You can do it in two ways.
您可以通过两种方式做到这一点。
Firstly, do some modification in layoutAttributesForItem
,
首先,做一些修改layoutAttributesForItem
,
get the current attributes's layout via the previous layoutAttributesForItem(at: IndexPath(item: indexPath.item - 1, section: indexPath.section))?.frame
.
通过上一个layoutAttributesForItem(at: IndexPath(item: indexPath.item - 1, section: indexPath.section))?.frame
.获取当前属性的布局。
layoutAttributesForItem(at:): This method provides on demand layout information to the collection view. You need to override it and return the layout attributes for the item at the requested indexPath.
layoutAttributesForItem(at:):此方法向集合视图提供按需布局信息。您需要覆盖它并在请求的 indexPath 处返回项目的布局属性。
Secondly, new some attributes via UICollectionViewLayoutAttributes(forCellWith: indexPath)
,layout them wherever you want.
其次,通过新的一些属性UICollectionViewLayoutAttributes(forCellWith: indexPath)
,将它们放置在你想要的任何地方。
And the math is a little larger, because it performs the heavy lifting of the layout.
数学有点大,因为它执行了布局的繁重工作。
layoutAttributesForElements(in:): In this method you need to return the layout attributes for all the items inside the given rectangle. You return the attributes to the collection view as an array of UICollectionViewLayoutAttributes.
layoutAttributesForElements(in:):在此方法中,您需要返回给定矩形内所有项目的布局属性。您将属性作为 UICollectionViewLayoutAttributes 数组返回到集合视图。
You can check My repo
你可以检查我的回购
- you can put items left aligned
- 您可以将项目左对齐
- you can put items right aligned
- 你可以把项目右对齐
- you can put items right aligned and items reversed
- 你可以把项目右对齐和项目反转
回答by Kevin R
A cleaner swift version for people interested, based on Chris Wagner's answer:
基于 Chris Wagner 的回答,为感兴趣的人提供了一个更简洁的 swift 版本:
##代码##