簡體   English   中英

在集合視圖中增加單元格之間的間距大小

[英]Increase size of spacing between cells in collection view

我想在collectionview中的單元格之間使用不同的間距。

我的收藏集視圖僅包含一個部分。 我知道可以更改部分之間的間距,但我會喜歡單元格之間的間隔。

我希望居中的單元格及其側面之間的間距更大,並且其他單元格的間距不要移動。 基本上,我的白色圓圈在不更改所有間距的情況下不會接觸相鄰單元的邊緣。

我的問題的圖片在這里:

我的問題的圖片在這里

有解決辦法嗎?

您可以在視圖控制器中實現此方法: collectionView:layout:minimumInteritemSpacingForSectionAtIndex

您可以嘗試創建3個不同的部分。

  1. 第一部分 -包含較大單元格之前的單元格的單元格,單元inter item spacing as 10
  2. 第二部分 -包含較大單元格的section insets as (top: 0, left: 20, bottom: 0, right: 20)section insets as (top: 0, left: 20, bottom: 0, right: 20)
  3. 第三部分 -包含較大單元格之后的單元格的部分, inter item spacing as 10

您可以根據需要更改值。

代碼是這樣的:

func numberOfSections(in collectionView: UICollectionView) -> Int
{
    return 3
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
    if section == 1
    {
        return 1
    }
    return 2
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
    if indexPath.section == 1
    {
        cell.backgroundColor = UIColor.red
    }
    return cell
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize
{
    if indexPath.section == 1
    {
        return CGSize(width: 100, height: 100)
    }
    return CGSize(width: 82, height: 82)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat
{
    if section == 1
    {
        return 0
    }
    return 5
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets
{
    if section == 1
    {
        return UIEdgeInsets(top: 0, left: 20, bottom: 0, right: 20)
    }
    return .zero
}

截圖:

在此處輸入圖片說明

讓我知道您是否仍然遇到任何問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM