LaVOZs

The World’s Largest Online Community for Developers

'; ios - Changing frame and contentInset of UICollectionView in layoutSubviews() using IGListKit - LavOzs.Com

I need to change the frame and top content inset (contentInset.top) of my collection view (which is a UICollectionView). The top inset and frame changes depend on the bounds of the superview and the content offset of the collection view, thus I put the inset-changing code in layoutSubviews().

override func layoutSubviews() {
  super.layoutSubviews()

  collectionView.collectionViewLayout.invalidateLayout()
  collectionView.contentInset.top = new_inset_top
  collectionView.frame = new_frame
}

However, the collection view does not account for the new insets and the log shows the following:

The behavior of the UICollectionViewFlowLayout is not defined because: the item height must be less than the height of the UICollectionView minus the section insets top and bottom values, minus the content insets top and bottom values.

How can I fix this to let the collection view displays correctly?

I am using IGListKit

You do not need to do all these inside of layoutSubviews.

in viewDidLoad implement it like following

func viewDidLoad() {
    super.viewDidLoad()

    // let say you want 40px top inset
    collectionView.contentInset = UIEdgeInsetsMake(40, 0, 0, 0)
}

Another thing is that, i don't get, why are you setting the frame in layoutSubviews. There is no need to set frame to change the contentInset.

Related
Status bar and navigation bar appear over my view's bounds in iOS 7
UIScrollView Scrollable Content Size Ambiguity
Why is UICollectionView insetForSectionsAtIndex not being called?
UICollectionViewFlowLayout exception when executing XHR-Request in NativeScript
UICollectionView destroyed in UITableViewCell
How to set height on UICollectionView / keep height of UICollectionView from expanding in simulator
UICollectionView full width and height problem