From 42f55d33d20c986f6dcdbb0cf67d1fb94c19fd2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Joa=CC=83o=20Pereira?= Date: Thu, 17 Aug 2017 15:11:43 +0100 Subject: [PATCH 1/3] Add selected image --- Segmentio/Source/Cells/SegmentioCell.swift | 8 +++++++- Segmentio/Source/Segmentio.swift | 6 ++++-- Segmentio/Source/SegmentioOptions.swift | 4 +++- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Segmentio/Source/Cells/SegmentioCell.swift b/Segmentio/Source/Cells/SegmentioCell.swift index af70d89..187e6fd 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,12 @@ class SegmentioCell: UICollectionViewCell { segmentTitleLabel?.textColor = selected ? selectedState.titleTextColor : defaultState.titleTextColor segmentTitleLabel?.font = selected ? selectedState.titleFont : defaultState.titleFont } + + backgroundColor = selected ? selectedState.backgroundColor : defaultState.backgroundColor + + 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 b4797b9..783eafc 100644 --- a/Segmentio/Source/Segmentio.swift +++ b/Segmentio/Source/Segmentio.swift @@ -536,14 +536,16 @@ 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..3addf9c 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 } public mutating func addBadge(_ count: Int, color: UIColor) { From 72c4974a4393f8e74b08f2906a79646c370b9879 Mon Sep 17 00:00:00 2001 From: Ilya Date: Fri, 1 Sep 2017 17:33:02 +0300 Subject: [PATCH 2/3] changes after review --- Segmentio/Source/Cells/SegmentioCell.swift | 2 +- Segmentio/Source/Segmentio.swift | 8 ++++++-- Segmentio/Source/SegmentioOptions.swift | 2 +- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Segmentio/Source/Cells/SegmentioCell.swift b/Segmentio/Source/Cells/SegmentioCell.swift index 187e6fd..3b83d1f 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, selectedImage:UIImage? = nil, image:UIImage? = nil) { + func configure(selected: Bool, selectedImage: UIImage? = nil, image: UIImage? = nil) { cellSelected = selected let selectedState = options.states.selectedState diff --git a/Segmentio/Source/Segmentio.swift b/Segmentio/Source/Segmentio.swift index f4da738..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 @@ -546,7 +546,11 @@ extension Segmentio: UICollectionViewDataSource { isLastCell: indexPath.row == segmentioItems.count - 1 ) - cell.configure(selected: (indexPath.row == selectedSegmentioIndex), selectedImage:content.selectedImage, image:content.image) + 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 3addf9c..2ca66ca 100644 --- a/Segmentio/Source/SegmentioOptions.swift +++ b/Segmentio/Source/SegmentioOptions.swift @@ -21,7 +21,7 @@ public struct SegmentioItem { public init(title: String?, image: UIImage?, selectedImage: UIImage? = nil) { self.title = title self.image = image - self.selectedImage = selectedImage + self.selectedImage = selectedImage ?? image } public mutating func addBadge(_ count: Int, color: UIColor) { From 9805a5106cc7a20aa71e92a1fe385d2ff33e2601 Mon Sep 17 00:00:00 2001 From: Ilya Date: Fri, 1 Sep 2017 18:51:14 +0300 Subject: [PATCH 3/3] remove instant backgounr color replacment --- Segmentio/Source/Cells/SegmentioCell.swift | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Segmentio/Source/Cells/SegmentioCell.swift b/Segmentio/Source/Cells/SegmentioCell.swift index 3b83d1f..c370468 100644 --- a/Segmentio/Source/Cells/SegmentioCell.swift +++ b/Segmentio/Source/Cells/SegmentioCell.swift @@ -140,9 +140,7 @@ class SegmentioCell: UICollectionViewCell { segmentTitleLabel?.textColor = selected ? selectedState.titleTextColor : defaultState.titleTextColor segmentTitleLabel?.font = selected ? selectedState.titleFont : defaultState.titleFont } - - backgroundColor = selected ? selectedState.backgroundColor : defaultState.backgroundColor - + if (style != .onlyLabel) { segmentImageView?.image = selected ? selectedImage : image }