sectionInset not working with UICollectionView estimatedItemSize in iOS8 using AutoLayout

1.4k views Asked by At

I can't get my UICollection to properly set sectionInset.left margin when using self-sizing cells with estimatedItemSize. Cells of the UICollectionview should have a somewhat fixed cell height but dynamic width according to the text length.

Here is how it looks when the self-sizing is enabled:

enter image description here

And when the self-sizing is disabled:

enter image description here

Oddly enough, it seems that sectionInset.right is working, i.e. the margin is added on the right hand side of the UICollectionView.

Here's my custom UICollectionViewFlowLayout, i'm toggling self-size mode using the comment.

class myFlow: UICollectionViewFlowLayout {
    required init(coder: NSCoder) {
    super.init(coder: coder)

        self.minimumLineSpacing = 1
//        self.estimatedItemSize = CGSize(width: 100, height: 35)
        self.sectionInset.left = 20
    }

And my custom UICollectionView:

class myCV: UICollectionView {
    required init(coder decoder: NSCoder) {
        super.init(coder: decoder)

        self.layer.cornerRadius = 5.0
        self.layer.borderWidth = 1.0
        self.layer.borderColor = UIColor.lightGrayColor().CGColor

    }
}

I've laid out the label inside the UICollectionView cell using AutoLayout:

enter image description here

1

There are 1 answers

0
Sergii Martynenko Jr On

I've found a work around this - use contentInset property of UIScrollView, that is super class of UICollectionView. It is not strictly saying is answer, but, at least, a solution of the problem