UICollectionViewCompositionalLayout cannot ignore safe area

Hi, I use simple UICollectionViewCompositionalLayout and cannot figure out how to force UICollectionView to not adjust content insets. I use very simple layout with cell of size of a screen with image view on it. I was able to achieve this with just FlowLayout.


Is there any way to force ignore safe area margins?

Replies

Same thing in 13.1beta 3

Unfortunately there is nothing in the API surface for compositional layout to allow for this, so (for now at least) you have to override manually.


Luckily it's very trivial to do:

class CollectionView : UICollectionView {

  private var _safeAreaInsets:UIEdgeInsets?
  override var safeAreaInsets:UIEdgeInsets {
    get { _safeAreaInsets ?? super.safeAreaInsets }
    set { _safeAreaInsets = newValue }
  }

}


Usage:

myCollectionView.safeAreaInsets = UIEdgeInsets()


REFERENCE: https://stackoverflow.com/a/59600751/24177

  • This is the only thing that works

Add a Comment
It's now possible on iOS and tvOS 14.
You have to use the contentInsetsReference property.

You can set for a specific NSCollectionLayoutSection:
Code Block swift
let section = NSCollectionLayoutSection(group: group)
section.contentInsetsReference = .none

Or you can set for the whole UICollectionViewCompositionalLayout using UICollectionViewCompositionalLayoutConfiguration:
Code Block swift
let configuration = UICollectionViewCompositionalLayoutConfiguration()
configuration.contentInsetsReference = .none


https://developer.apple.com/documentation/uikit/uicontentinsetsreference
  • For 2022, particularly for TVOS,

    this does not actually do anything, it seems to just set the "mode" but not literally change the values.

    The only actual solution seems to be the mbradea solution, just override safeAreaInsets. Thanks!

Add a Comment