2017-03-17 73 views
1
删除多余的空间

Desired Output like this image在集合视图中斯威夫特

代码:

let reuseIdentifier = "cell" 
var items = ["1", "2", "3", "4","5","6","7","8", "9", "10", "11","12","13","14"] 

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
    return self.items.count 
} 

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! MyCollectionViewCell 
    // Use the outlet in our custom class to get a reference to the UILabel in the cell 
    cell.myLabel.text = self.items[indexPath.item] 
    cell.backgroundColor = UIColor.red 
    return cell 
} 

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 
    return CGSize(width: CGFloat((collectionView.frame.size.width/2) - 8), height: CGFloat(250)) 
} 
+1

图片不可见 –

+0

可以粘贴截图吗? –

+0

您应该解释您遇到的问题并显示您尝试解决的问题。横轴上是否有额外的空间?垂直轴?细胞之间?部分之间?请详细说明,以便您可以帮助我们为您提供帮助。 – Laffen

回答

0

这根据你的要求做希望这将有助于你得到一个想法。

let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout() 
      layout.sectionInset = UIEdgeInsets(top: 10, left: 1, bottom: 5, right: 0) // according to your requirements 
      layout.minimumInteritemSpacing = 1 // according to your requirements 
      layout.minimumLineSpacing = 1// according to your requirements 

如果你想要更多的帮助,或者这不是为你工作,你也可以通过link它可以帮助你实现你的目标。点击这里link

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