[英]collection view cell Hight and width in swift
collection view had multiple images working fine. 集合视图中有多个图像可以正常工作。 it's showing like this
像这样显示
But i want show like this way 但是我想这样表演
this is code of collection view cell 这是集合视图单元格的代码
let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
layout.sectionInset = UIEdgeInsets(top: 10, left: 0, bottom: 20, right: 0)
let width = UIScreen.main.bounds.width
layout.itemSize = CGSize(width: width/2 , height: width/2 )
layout.minimumInteritemSpacing = 0
layout.minimumLineSpacing = 0
collectionView!.collectionViewLayout = layout
Just implement some UICollectionViewDelegateFlowLayout
methods to get the correct size and spacing of the UICollectionViewCells
. 只是执行一些
UICollectionViewDelegateFlowLayout
方法来获取正确的大小和间距UICollectionViewCells
。
Example: 例:
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
let cellSize = CGSize(width: (collectionView.bounds.width - (3 * 10))/2, height: 120)
return cellSize
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat
{
return 10
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
{
let sectionInset = UIEdgeInsetsMake(10, 10, 10, 10)
return sectionInset
}
Just change the size and spacing values according to your requirement. 只需根据您的要求更改大小和间距值。
Add this method on your viewcontroller 在您的ViewController上添加此方法
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
return CGSize(width: ((self.view.frame.size.width/2) - 10), height: 255)
}
尝试,
collectionViewCellImageView.contentMode = .scaleToFill
You can try to set minimum spacing for cell & line for collection view and implement the bellow delegate 您可以尝试为集合视图设置单元格和行的最小间距,并实现波纹管委托
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: (collectionView.frame.width/2) - 5, height: collectionView.frame.width/2)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.