Justify last line of UICollectionView

2020-01-29 10:02发布

As far as I can see, UICollectionViewFlowLayout justifies all the lines of a section except the last one. So for example, if there aren't enough items to fill more than 1 line, the result is not justified.

enter image description here

What is the simplest way to either configure or subclass UICollectionViewFlowLayout so that it also justifies the last line of a section?

enter image description here

2条回答
再贱就再见
2楼-- · 2020-01-29 10:32

This is not really possible, unfortunately. It's just not designed that way, unfortunately.

One thing I can think of that might work though is to add an extra item into the data source, returned at the end, which has a cell that is 0 pixels tall and the width of the collection view. That will force the last row to be a single row with just that one item on it and then all rows above it will be justified.

查看更多
太酷不给撩
3楼-- · 2020-01-29 10:37

You can do this by subclassing UICollectionViewFlowLayout and overriding layoutAttributesForElementsInRect: and layoutAttributesForItemAtIndexPath: to place all the items in the last line (except the first, which is already correct) where you want them, by changing the frame property of the item's UICollectionViewLayoutAttributes object.

查看更多
登录 后发表回答