How to apply different contentInset only for content cells, but not for the title in a UICollectionView

I use the insetForSectionAtIndex method to set the contentInset for the section in my collection view, and I do not want to apply this insertion to the section title. I need the title width as the screen width.

Conentinset

func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAtIndex section: Int) -> UIEdgeInsets {
    if section == 1 {
        return UIEdgeInsets(top: 0, left: 15, bottom: 0, right: 15)
    }

    return UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
}

Headline

override func collectionView(collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, atIndexPath indexPath: NSIndexPath) -> UICollectionReusableView {

    let header = collectionView.dequeueReusableSupplementaryViewOfKind(kind, withReuseIdentifier: opsMainDescriptionSegmentedControlCellId, forIndexPath: indexPath) as! MyHeader

    return header
}
+4
source share
2 answers

I just needed to do the same in one of my projects.

The solution I applied:

At Storyboard, my collection view spans the entire screen with Auto-Layout restrictions.

In view mode DidLoad:

collectionView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)

I set collectionView delegate for myself, and also compatible with

UICollectionViewDelegateFlowLayout.

, , UIEdgeInsets

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {

        return UIEdgeInsets(top: 15, left: 20, bottom: 15, right: 20)

    }

.

, .

enter image description here

, .

+4

. UICollectionViews , UICollectionView. , (), UICollectionView - , UIView . , , UICollectionView.

0

Source: https://habr.com/ru/post/1648462/


All Articles