简体   繁体   中英

Section Insets not respected when rendering the cells of UICollectionView

I have a UICollectionView containing two sections. I have given section insets of left = 5 and right = 5 from storyboard. I am using the same .xib for cells of both the sections. I am inputting the size of the cells in sizeForItemAtIndexPath

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {

        let itemWidth = self.collectionViewTrending.bounds.size.width - 10
        if(indexPath.section == 0) {

                return CGSizeMake(itemWidth , 200)

        } else {

            let product = self.products.objectAtIndex(indexPath.row) as! Product
            let imageHeight = CGFloat(product.imageHeight)
            return CGSizeMake(itemWidth, imageHeight * 0.81)
        }



   }

The problem is this :

For Section 0, the cell is centered and section insets are respected.

For Section 1, the left section inset is respected but the right isn't and all the cells of section 1 have their right edges touching the right edge of collectionView.

Why is this happening and how can I resolve it?

Section insets apply to the section as a whole:

The left inset affects the space between the left edge of the section and the left edge of the FIRST cell in that section.

The right inset affects the space between the right edge of the LAST cell in the section and the right edge of that section.

In other words, the behavior you're seeing is probably because you have only one cell in section 0, and multiple cells in section 1.

What you probably want is space between cells to create the appearance of a border around each cell.

To do this, set the Min Spacing / For Cells attribute of the collection view in your storyboard. Or set this dynamically in the collectionView minimumInteritemSpacingForSectionAtIndex delegate method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM