Different margins with `UIListContentView` in subclass vs. using `UICollectionViewListCell` directly
I get different to the top and bottom of the cell when I use UIListContentView in a subclass versus if I use UICollectionViewListCell directly.
Here's how I setup directly UICollectionViewListCell:
let reg = UICollectionView.CellRegistration<UICollectionViewListCell, EventCellViewModel> { cell, indexPath, model in
var content = cell.defaultContentConfiguration()
content.text = model.titleText
content.secondaryText = model.subtitleText
var accessories: [UICellAccessory] = [.disclosureIndicator(displayed: .always, options: .init(tintColor: .nicePurpleColor))]
accessories.append(.customView(configuration: .init(customView: UIImageView(image: UIImage(systemName: "bell")), placement: .trailing())))
cell.accessories = accessories
cell.contentConfiguration = content
}
Here's how I use it in my subclass:
Initialize it
private lazy var listContentView = UIListContentView(configuration: defaultListContentConfiguration())
Setup the views:
private func setupViewsIfNeeded() {
guard accessories.isEmpty else { return }
accessories = [
.disclosureIndicator(displayed: .always, options: .init(tintColor: .nicePurpleColor)),
.customView(configuration: .init(customView: sswitch, placement: .trailing()))
]
contentView.addSubview(listContentView)
listContentView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
listContentView.topAnchor.constraint(equalTo: contentView.topAnchor),
listContentView.leftAnchor.constraint(equalTo: contentView.leftAnchor),
listContentView.rightAnchor.constraint(equalTo: contentView.rightAnchor),
listContentView.bottomAnchor.constraint(equalTo: contentView.bottomAnchor),
])
}
and then lastly configure it:
override func updateConfiguration(using state: UICellConfigurationState) {
setupViewsIfNeeded()
var content = defaultListContentConfiguration().updated(for: state)
content.text = state.viewModel?.titleText
content.secondaryText = state.viewModel?.subtitleText
content.axesPreservingSuperviewLayoutMargins = [.both]
listContentView.configuration = content
sswitch.isOn = state.viewModel?.isEnabledForCalendar ?? false
}
/best regards, David