集合视图单元格swift中的高度和宽度

时间:2017-08-11 07:20:16

标签: ios swift swift3 uicollectionview

集合视图有多个图像正常工作。它显示如下

enter image description here

但我想要这样的表演

enter image description here

这是集合视图单元格的代码

 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

4 个答案:

答案 0 :(得分:3)

只需实施一些UICollectionViewDelegateFlowLayout方法即可获得UICollectionViewCells的正确大小和间距。

示例:

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
}

只需根据您的要求更改尺寸和间距值。

答案 1 :(得分:2)

在viewcontroller上添加此方法

cts:highlight

答案 2 :(得分:0)

尝试,

$sce

答案 3 :(得分:0)

您可以尝试设置细胞和细胞的最小间距。收集视图的行并实现波纹管代表

class MyModelView(BaseModelView):
    column_list = ('name', 'last_name', 'email')
    form_widget_args = {
        'email':{
            'readonly':True
        }
    }