5
votes

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
I tested this again with XCode 7 beta 1 and it's working with that release so i assume it's a bug in Apple's current implementation of self-sizing cells for UICollectionView. Another issue i had was about getting random out of bounds exceptions with index references like 9223372036854775806 which i traced down to be caused by estimatedItemSize as well. - petard
I just met a very similar bug. The left inset was reported to the right inset for a view really similar to yours ( UiCollectionView, horizontal mode) when I use estimatedItemSize. - alaeri
I have the same problem but for all insets. My problem is a bit worst because I need to set different content sizes to different sections. And actually, the collection view doesn't even display itself if I set the content inset to be different than 0,0,0,0. Even if 1, 0, 0, 0... - Nuno Gonçalves

1 Answers

0
votes

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