如何以编程方式在集合视图中添加 Section Insect?

how to add Section Insect in collection view programmatically?

我想根据“Left Aligned collection View Flow Layout”将 Section Insets 添加到我的集合视图中

我使用了下面的代码但没有用,

  @IBOutlet private weak var collection: UICollectionView!
collectionView.contentInset = UIEdgeInsets(top: 60, left: 0, bottom: 0, right: 0)

contentInset 不是 sectionInset。这是您可以分配 sectionInset.

的方法
if let flowLayout = collectionView.collectionViewLayout as? UICollectionViewFlowLayout {
    flowLayout.sectionInset = UIEdgeInsets(top: 60, left: 0, bottom: 0, right: 0)
}