How to show three columns in a CollectionView using swift4 in all devices

杀马特。学长 韩版系。学妹 提交于 2020-01-06 05:41:10

问题


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)
    }


}


回答1:


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

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)
    }
}

Here 60 means SPACE 20 CELL SPACE 20 CELL SPACE 20



来源:https://stackoverflow.com/questions/49795872/how-to-show-three-columns-in-a-collectionview-using-swift4-in-all-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!