diff --git a/Segmentio/Source/Cells/SegmentioCell.swift b/Segmentio/Source/Cells/SegmentioCell.swift index af70d89..c370468 100644 --- a/Segmentio/Source/Cells/SegmentioCell.swift +++ b/Segmentio/Source/Cells/SegmentioCell.swift @@ -130,7 +130,7 @@ class SegmentioCell: UICollectionViewCell { configurateBadgeWithCount(content.badgeCount, color: content.badgeColor) } - func configure(selected: Bool) { + func configure(selected: Bool, selectedImage: UIImage? = nil, image: UIImage? = nil) { cellSelected = selected let selectedState = options.states.selectedState @@ -140,6 +140,10 @@ class SegmentioCell: UICollectionViewCell { segmentTitleLabel?.textColor = selected ? selectedState.titleTextColor : defaultState.titleTextColor segmentTitleLabel?.font = selected ? selectedState.titleFont : defaultState.titleFont } + + if (style != .onlyLabel) { + segmentImageView?.image = selected ? selectedImage : image + } } func configurateBadgeWithCount(_ badgeCount: Int?, color: UIColor?) { diff --git a/Segmentio/Source/Segmentio.swift b/Segmentio/Source/Segmentio.swift index 578442e..74c5638 100644 --- a/Segmentio/Source/Segmentio.swift +++ b/Segmentio/Source/Segmentio.swift @@ -96,7 +96,7 @@ open class Segmentio: UIView { collectionView.bounces = true collectionView.isScrollEnabled = segmentioOptions.scrollEnabled collectionView.backgroundColor = .clear - collectionView.accessibilityIdentifier = "segmentio_collection_view" + collectionView.accessibilityIdentifier = "segmentio_collection_view" segmentioCollectionView = collectionView @@ -537,14 +537,20 @@ extension Segmentio: UICollectionViewDataSource { withReuseIdentifier: segmentioStyle.rawValue, for: indexPath) as! SegmentioCell + let content = segmentioItems[indexPath.row] + cell.configure( - content: segmentioItems[indexPath.row], + content: content, style: segmentioStyle, options: segmentioOptions, isLastCell: indexPath.row == segmentioItems.count - 1 ) - cell.configure(selected: (indexPath.row == selectedSegmentioIndex)) + cell.configure( + selected: (indexPath.row == selectedSegmentioIndex), + selectedImage: content.selectedImage, + image: content.image + ) return cell } diff --git a/Segmentio/Source/SegmentioOptions.swift b/Segmentio/Source/SegmentioOptions.swift index 9c6f641..2ca66ca 100644 --- a/Segmentio/Source/SegmentioOptions.swift +++ b/Segmentio/Source/SegmentioOptions.swift @@ -14,12 +14,14 @@ public struct SegmentioItem { public var title: String? public var image: UIImage? + public var selectedImage: UIImage? public var badgeCount: Int? public var badgeColor: UIColor? - public init(title: String?, image: UIImage?) { + public init(title: String?, image: UIImage?, selectedImage: UIImage? = nil) { self.title = title self.image = image + self.selectedImage = selectedImage ?? image } public mutating func addBadge(_ count: Int, color: UIColor) {