gpt4 book ai didi

uicollectionview - 如何将 UICollectionView 的单元格水平和垂直居中?

转载 作者:行者123 更新时间:2023-12-04 08:26:52 28 4
gpt4 key购买 nike

如何将 UICollectionView 中的所有单元格垂直和水平居中使用 UICollectionViewFlowLayout ?

流布局根据滚动方向在右侧或底部留下间距。我想在所有方面设置相同的填充。我玩过sectionInset UICollectionViewFlowLayout 的属性(property).

最佳答案

我建议实现 UICollectionViewDelegateFlowLayout协议(protocol)以动态实现单元格的水平和垂直居中。来自 UICollectionViewDelegateFlowLayout 协议(protocol)引用 :

The UICollectionViewDelegateFlowLayout protocol defines methods that let you coordinate with a UICollectionViewFlowLayout object to implement a grid-based layout. The methods of this protocol define the size of items and the spacing between items in the grid.

All of the methods in this protocol are optional. If you do not implement a particular method, the flow layout delegate uses values in its own properties for the appropriate spacing information.

The flow layout object expects the collection view’s delegate object to adopt this protocol. Therefore, implement this protocol on object assigned to your collection view’s delegate property.


您可能对 collectionView:layout:insetForSectionAtIndex: 最感兴趣可以使用类似 UIEdgeInsetsMake 的方法来调整屏幕所有四个侧面的插图的方法.您可以通过从相应的屏幕尺寸(垂直或水平)中减去适当的数量(例如单元格的大小)来根据屏幕尺寸确定插图。然后,除以 2 以获得特定维度的相等插图。

关于uicollectionview - 如何将 UICollectionView 的单元格水平和垂直居中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15355820/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com