Skip to content

Commit

Permalink
Add support for white search icon
Browse files Browse the repository at this point in the history
  • Loading branch information
YuigaWada committed Apr 30, 2020
1 parent d0f3607 commit 980bf86
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion PolioPager.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = "PolioPager"
s.version = "2.2"
s.version = "2.5"
s.summary = "PolioPager enables us to use PagerTabStrip like SNKRS."
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.homepage = "https://github.com/yuigawada/PolioPager"
Expand Down
30 changes: 29 additions & 1 deletion PolioPager/PolioPagerViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ open class PolioPagerViewController: UIViewController, TabCellDelegate, PolioPag
// Tab
public var items: [TabItem] = []
public var needSearchTab: Bool = true
public var searchIconColor: SearchIconColor = .black
public var initialIndex: Int = 0
public var tabBackgroundColor: UIColor = .white

Expand Down Expand Up @@ -405,12 +406,32 @@ open class PolioPagerViewController: UIViewController, TabCellDelegate, PolioPag
guard needSearchTab, items.filter({$0.isSearchTab}).count == 0 else { return }

let searchItem = TabItem(isSearchTab: true,
image: UIImage(named: "search", in: bundle, compatibleWith: nil),
image: getSearchIcon(),
cellWidth: 20)

items.insert(searchItem, at: 0)
}

private func getSearchIcon()-> UIImage? {
guard let image = UIImage(named: "search", in: bundle, compatibleWith: nil) else { return nil }
return searchIconColor == .white ? reverseBW(image) : image
}

private func reverseBW(_ original: UIImage)-> UIImage {
guard let cgImage = original.cgImage,
let filter = CIFilter(name: "CIColorInvert") else { return original }

let image = CIImage(cgImage: cgImage)
filter.setDefaults()
filter.setValue(image, forKey: kCIInputImageKey)

let context = CIContext(options: nil)
if let imageRef = context.createCGImage(filter.outputImage!, from: image.extent) {
return UIImage(cgImage: imageRef)
}
return original
}

private func setTabItem(_ items: [TabItem]) {
self.items = items
setSearchTab()
Expand Down Expand Up @@ -500,6 +521,13 @@ open class PolioPagerViewController: UIViewController, TabCellDelegate, PolioPag
}
}

extension PolioPagerViewController {
public enum SearchIconColor {
case black
case white
}
}

extension PolioPagerViewController: UICollectionViewDataSource, UICollectionViewDelegateFlowLayout
{
public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
Expand Down

0 comments on commit 980bf86

Please sign in to comment.