任何其他想法?
解决方法 更新:Swift版本的这个答案: https://github.com/fanpyi/UICollectionViewLeftAlignedLayout-Swift采取@matt’s lead我修改他的代码,以确保项目总是左对齐。我发现如果一个项目自己在一行上结束,它将以流布局为中心。我做了以下更改以解决此问题。
这种情况只会发生,如果你有宽度不同的单元格,这可能导致一个布局,如下所示。最后一行总是左对齐由于UICollectionVIEwFlowLayout的行为,问题在于项本身是在任何行,但最后一个。
使用@ matt的代码,我看到。
在这个例子中,我们看到,如果细胞自己在线上结束,它们将居中。下面的代码确保您的集合视图看起来像这样。
#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]; if (indexPath.item == 0) { // first item of section CGRect frame = currentItemAttributes.frame; frame.origin.x = sectionInset.left; // first item of the section should always be left aligned currentItemAttributes.frame = frame; return currentItemAttributes; } NSIndexPath* prevIoUsIndexPath = [NSIndexPath indexPathForItem:indexPath.item-1 inSection:indexPath.section]; CGRect prevIoUsFrame = [self layoutAttributesForItemAtIndexPath:prevIoUsIndexPath].frame; CGfloat prevIoUsFrameRightPoint = prevIoUsFrame.origin.x + prevIoUsFrame.size.wIDth + kMaxCellSpacing; CGRect currentFrame = currentItemAttributes.frame; 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 CGRect frame = currentItemAttributes.frame; frame.origin.x = sectionInset.left; // first item on the line should always be left aligned currentItemAttributes.frame = frame; return currentItemAttributes; } CGRect frame = currentItemAttributes.frame; frame.origin.x = prevIoUsFrameRightPoint; currentItemAttributes.frame = frame; return currentItemAttributes;}@end总结
以上是内存溢出为你收集整理的iphone – 你如何确定UICollectionView flowLayout中的单元格之间的间距全部内容,希望文章能够帮你解决iphone – 你如何确定UICollectionView flowLayout中的单元格之间的间距所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)