[英]Swift - Change UICollectionViewCell size programmatically
I am struggling to change the size my items in the CollectionView.我正在努力更改 CollectionView 中我的项目的大小。
Custom Flowlayout class found here :自定义流程布局 class在这里找到:
This is my CollectionView:这是我的收藏视图:
class ExampleViewController: UIViewController, UICollectionViewDataSource {
let theCollectionView: UICollectionView = {
let v = UICollectionView(frame: CGRect.zero, collectionViewLayout: UICollectionViewFlowLayout())
v.translatesAutoresizingMaskIntoConstraints = false
v.backgroundColor = .white
v.contentInsetAdjustmentBehavior = .always
v.layer.cornerRadius = 30
return v
}()
let columnLayout = FlowLayout(
itemSize: CGSize(width: 50, height: 50),
minimumInteritemSpacing: 10,
minimumLineSpacing: 10,
sectionInset: UIEdgeInsets(top: 20, left: 20, bottom: 10, right: 20)
)
I tried changing the itemSize but that doesnt do anything.我尝试更改itemSize但这并没有做任何事情。
columnLayout
is not used, you need to replace UICollectionViewFlowLayout()
with it.没有使用
columnLayout
,你需要用它替换UICollectionViewFlowLayout()
。
let v = UICollectionView(frame: CGRect.zero, collectionViewLayout: columnLayout)
Here is Code for CollectionViewCell Size Change这是 CollectionViewCell 大小更改的代码
(Note:- Change values according to your Requirement!) (注意:- 根据您的要求更改值!)
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 20, left: 0, bottom: 10, right: 0)
layout.itemSize = CGSize(width: screenWidth/4, height: screenWidth/4)
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
cell2.collectionView2.collectionViewLayout = layout
You didn't assigned you flowLayout
to the collectionView
either try:您没有将
flowLayout
分配给collectionView
尝试:
theCollectionView.collectionViewLayout = columnLayout
or theCollectionView.collectionViewLayout = columnLayout
或
First define your layout:首先定义你的布局:
let columnLayout = FlowLayout(...
Then define your collectionView like this:然后像这样定义你的collectionView:
let theCollectionView: UICollectionView = { let v = UICollectionView(frame: CGRect.zero, collectionViewLayout:columnLayout)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.