我正在尝试创建一个如下所示的UI。
为此,我使用UICollectionView
和FlowLayout
。
为了将第一个具有全宽的单元格显示为3列,其余单元格显示为,我实现了sizeForItemAtIndexPath:
和minimumInteritemSpacingForSectionAtIndex
方法:
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
{
var cellSize = CGSizeZero
if indexPath.item == 0
{
let width = self.cvMedia.frame.width - 20
let height = (width * 9)/16
cellSize = CGSize(width: width, height: height)
}
else
{
let width = self.cvMedia.frame.width / 3 - 20
let height = (width * 16)/9
cellSize = CGSize(width: width, height: height)
}
return cellSize
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat
{
return 20.0
}
但是我在iPhone 6SPlus和iPhone 6Plus中得到以下输出:
I tried by changing the item spacing from 20 to 19 and it worked on simulator, but on actual device it still shows the same behaviour. If I change the spacing values I'm getting the correct output on some device versions, and not working on some versions. I can add a device version check and based on that I can return the value. But it is not an elegant solution and it will break on future versions. Can anyone help me to solve this issue ? Thanks in advance.
You should use the collection view's width as your parameter when making this layout.
For instance,
Solution:
Calculate the size of the cell at runtime either on viewWillAppear() or cellForRowAtIndexPath()
Scenario:
设备的宽度是320pt。Collectionview的宽度为300pt(左右间距10pt)间距:单元格之间为20pt。电池需要3个!单元格大小:
现在..开始使用任何一种方法进行计算。
像元间距=(3-1)* 20 = 2 * 20 = 40pt
其余集合视图宽度= 300pt-40pt = 260pt
像元大小= 260pt / 3 = 86.667 pt。(约86pt)。
在代码中:
cellWidth = ( collectionView?.bounds.width - minimumSpacing * (numberOfCellsInARow - 1 ) ) / numberOfCellsInARow
同样,这仅给出cellWidth。您还需要cellHeight。您可能已经具有该单元格的长宽比。使用它,还计算cellHeight。
亲切的问候,
苏曼·阿迪卡里(Suman Adhikari)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句