How to change a UICollectionViewListCell’s separator inset
Published on: June 25, 2020In WWDC2020's session Lists in UICollectionView a slide is shown where a UICollectionViewListCell
's separator inset is updated by assigning a new leading anchor to separatorLayoutGuide.leadingAnchor
.
Unfortunately, this doesn't work in when you try to do it.
To set the separator inset for a UICollectionViewListCell
you can update the leading anchor constraint by overriding updateConstraints
in a UICollectionViewListCell
subclass. Setting the anchor in init
will cause the system to override your custom anchor leaving you with the default inset.
override func updateConstraints() {
super.updateConstraints()
separatorLayoutGuide.leadingAnchor.constraint(equalTo: someOtherView.leadingAnchor, constant: 10).isActive = true
}
You can set the leadingAnchor
constraint just like you would set any other constraint. In fact, you can even set the separator's trailingAnchor
using the same method I just showed if you want to offset the seperator from the trailing edge of your content view.