如何在所有设备中使用 swift4 在 CollectionView 中显示三列 [英] How to show three columns in a CollectionView using swift4 in all devices

查看:27
本文介绍了如何在所有设备中使用 swift4 在 CollectionView 中显示三列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    
    
    if collectionView == self.collectionViewVideo {

        var collectionViewSize = collectionViewVideo.frame.size
        collectionViewSize.width = collectionViewSize.width/3.0 //Display Three elements in a row.
        return collectionViewSize
    } else {
        return CGSize(width: 60, height: 60)
    }
    
    
}

推荐答案

您还需要考虑空间.空间是你的收藏视图项目空间

You also need to take space in account. Space is your collection view item space

以下是代码有两个单元格和 20 个空格.(我不是在写你需要的确切代码你应该自己做)

Following is the code has two cells and 20 spaces. (I am not writing exactly code you need You should do it by your self)

extension YourViewController : UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 20
    }

    //--------------------------------------------------------------------------------

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 20;
    }

    //--------------------------------------------------------------------------------

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

    //--------------------------------------------------------------------------------

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
       return UIEdgeInsetsMake(0, 20, 20, 20)
    }
}

这里60表示SPACE 20 CELL SPACE 20 CELL SPACE 20

Here 60 means SPACE 20 CELL SPACE 20 CELL SPACE 20

这篇关于如何在所有设备中使用 swift4 在 CollectionView 中显示三列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆