如何在所有设备中使用swift4在CollectionView中显示两列

在此处输入图像描述

要在collectionView中只显示两列,我正在使用这段代码

let layout = self.collectionView.collectionViewLayout as! UICollectionViewFlowLayout layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0) layout.minimumInteritemSpacing = 4 layout.itemSize = CGSize(width:(self.collectionView.frame.size.width - 10)/2,height: (self.collectionView.frame.size.height)/3) 

但它只在5s上正常显示,而不是在每个iphone上显示。 请帮忙。

我想像这个图像一样显示我的视图控制器。 请帮助任何人

在此处输入图像描述

将此代码添加到viewDidLoad 。 根据你改变高度。

尝试这个!

 if let layout = collectionView?.collectionViewLayout as? UICollectionViewFlowLayout{ layout.minimumLineSpacing = 10 layout.minimumInteritemSpacing = 10 layout.sectionInset = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10) let size = CGSize(width:(collectionView!.bounds.width-30)/2, height: 250) layout.itemSize = size } 

您必须缺少UICollectionViewDelegateFlowLayout

试试看,看看:

 // Source code import UIKit class CollectionVC: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { @IBOutlet var collection: UICollectionView! override func viewDidLoad() { super.viewDidLoad() collection.delegate = self collection.dataSource = self } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) //collection.reloadData() } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let flowayout = collectionViewLayout as? UICollectionViewFlowLayout let space: CGFloat = (flowayout?.minimumInteritemSpacing ?? 0.0) + (flowayout?.sectionInset.left ?? 0.0) + (flowayout?.sectionInset.right ?? 0.0) let size:CGFloat = (collection.frame.size.width - space) / 2.0 return CGSize(width: size, height: size) } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return 50 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "testcell", for: indexPath) cell.backgroundColor = UIColor.red return cell } } 

//界面设计

在此处输入图像描述

结果:

在此处输入图像描述

确保您的协议确认您的ViewController

 class ViewController: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout 

并将流程布局设置为您的collectionview对象

viewDidLoad中

 let layout = UICollectionViewFlowLayout() layout.scrollDirection = .vertical //.horizontal layout.minimumLineSpacing = 5 layout.minimumInteritemSpacing = 5 collectionView.setCollectionViewLayout(layout, animated: true) 

并实现以下方法

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { return UIEdgeInsets(top: 1.0, left: 1.0, bottom: 1.0, right: 1.0)//here your custom value for spacing } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let lay = collectionViewLayout as! UICollectionViewFlowLayout let widthPerItem = collectionView.frame.width / 2 - lay.minimumInteritemSpacing return CGSize(width:widthPerItem, height:100) } 

我有以下代码,我已经实现了在集合视图中显示两列。

添加UICollectionViewDelegateFlowLayout的以下方法方法:

 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let size = (collectionView.frame.size.width - 30) / 2 return CGSize(width: size, height: size) } 

其中30代表细胞之间的左,右和中间空间。

即,Cell应该在左侧添加10个像素,在右侧添加10个像素,在两个单元格之间添加10个像素。

从故事板中选择collectionview视图,并从最小间距的大小检查器集中选择。

我希望这能解决你的问题。

如果有任何疑问,请告诉我。

在此处输入图像描述