强制 collectionView 只有 2 行 [英] Enforce collectionView to have only 2 rows
本文介绍了强制 collectionView 只有 2 行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我必须创建一个集合视图,这样无论 iphone 大小如何,我们每行只有 2 个图像,而且我们需要每行和列之间的边框,如图所示.
I have to make one collection view so that irrespective of iphone size we have just 2 images in each row and also we need border between each row and column as shown in the image.
我想要这样:
推荐答案
试试这个代码.只是一种不同的方法.
Try this code. Just a different approach.
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: IndexPath) -> CGSize {
let layout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
layout.sectionInset = UIEdgeInsets(top: 6, left: 4, bottom: 6, right: 4)
layout.minimumInteritemSpacing = 04
layout.minimumLineSpacing = 04
layout.invalidateLayout()
return CGSize(width: ((self.view.frame.width/2) - 6), height: ((self.view.frame.width / 2) - 6))
}
以上代码在不同设备上的输出.
Output from above code on different devices.
这篇关于强制 collectionView 只有 2 行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文