Hi,
This is how I'm currently configuring an UICollectionView sidebar cell.
let rowRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, SidebarItem> {
(cell, indexPath, item) in
var contentConfiguration = UIListContentConfiguration.sidebarSubtitleCell()
contentConfiguration.text = item.title
contentConfiguration.secondaryText = item.subtitle
contentConfiguration.image = item.image
cell.contentConfiguration = contentConfiguration
}
In order to specify the selection background color of the cell I'm setting the backgroundConfiguration property. This work as expected:
let rowRegistration = UICollectionView.CellRegistration<UICollectionViewListCell, SidebarItem> {
(cell, indexPath, item) in
var contentConfiguration = UIListContentConfiguration.sidebarSubtitleCell()
contentConfiguration.text = item.title
contentConfiguration.secondaryText = item.subtitle
contentConfiguration.image = item.image
cell.contentConfiguration = contentConfiguration
var backgroundConfiguration = UIBackgroundConfiguration.listSidebarCell()
backgroundConfiguration.backgroundColorTransformer = UIConfigurationColorTransformer { [weak cell] (color) in
if let state = cell?.configurationState {
if state.isSelected || state.isHighlighted {
return UIColor(named: "Primary")!
}
}
return .clear
}
cell.backgroundConfiguration = backgroundConfiguration
}
Now I also need to change the text color based on the cell selection. When the cell is selected the text should be white. I tried to set the colorTransformer of textProperties of contentConfiguration like so:
contentConfiguration.textProperties.colorTransformer = UIConfigurationColorTransformer { [weak cell] (color) in
if let state = cell?.configurationState {
if state.isSelected || state.isHighlighted {
return .white
}
}
return .black
}
Unfortunately the text color does not change after the cell has been selected.
What am I doing wrong? Can anyone help me?
Thank you