Skip to content
This repository has been archived by the owner on Apr 11, 2021. It is now read-only.

Commit

Permalink
Fixed issue with changing tintColor not been applied as underline (wh…
Browse files Browse the repository at this point in the history
…ich marks selected segment) color.
  • Loading branch information
GocePetrovski committed Jan 27, 2017
1 parent b64cb14 commit dbaccbb
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11542" systemVersion="16B2555" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="kHm-QZ-Lrg">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11762" systemVersion="16D32" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="kHm-QZ-Lrg">
<device id="retina4_7" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11524"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand All @@ -17,9 +17,10 @@
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
<view key="tableHeaderView" contentMode="scaleToFill" id="sFi-9r-jXX" customClass="ScrollableSegmentedControl" customModule="ScrollableSegmentedControl">
<rect key="frame" x="0.0" y="64" width="375" height="49"/>
<rect key="frame" x="0.0" y="0.0" width="375" height="49"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="backgroundColor" red="0.90196079019999997" green="0.90196079019999997" blue="0.90196079019999997" alpha="1" colorSpace="calibratedRGB"/>
<color key="tintColor" red="0.0" green="0.50196081400000003" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
</view>
<sections>
<tableViewSection id="2bw-rH-tVL">
Expand Down
2 changes: 1 addition & 1 deletion ScrollableSegmentedControl/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0.1</string>
<string>1.0.2</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
Expand Down
11 changes: 11 additions & 0 deletions ScrollableSegmentedControl/ScrollableSegmentedControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ public class ScrollableSegmentedControl: UIControl {
flowLayout.minimumLineSpacing = 0

collectionView = UICollectionView(frame: frame, collectionViewLayout: flowLayout)
collectionView!.tag = 1
collectionView!.tintColor = tintColor
collectionView!.register(TextOnlySegmentCollectionViewCell.self, forCellWithReuseIdentifier: CollectionViewController.textOnlyCellIdentifier)
collectionViewController = CollectionViewController(segmentedControl: self)
Expand Down Expand Up @@ -320,6 +321,9 @@ public class ScrollableSegmentedControl: UIControl {
}

segmentCell.showUnderline = segmentedControl.underlineSelected
if segmentedControl.underlineSelected {
segmentCell.tintColor = segmentedControl.tintColor
}

return segmentCell
}
Expand Down Expand Up @@ -350,6 +354,7 @@ public class ScrollableSegmentedControl: UIControl {
underlineView?.removeFromSuperview()
} else {
underlineView = UIView()
underlineView!.tag = 999
underlineView!.backgroundColor = tintColor
underlineView!.isHidden = !isSelected
contentView.insertSubview(underlineView!, at: contentView.subviews.count)
Expand All @@ -360,6 +365,12 @@ public class ScrollableSegmentedControl: UIControl {
}
}

override var tintColor: UIColor!{
didSet{
underlineView?.backgroundColor = tintColor
}
}

override init(frame: CGRect) {
super.init(frame: frame)
configure()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ class ScrollableSegmentedControlTests: XCTestCase {

override func setUp() {
super.setUp()
// Put setup code here. This method is called before the invocation of each test method in the class.
segmentedControl.frame = CGRect(x: 0, y: 0, width: 320, height: 100)
segmentedControl.layoutSubviews()
}

override func tearDown() {
Expand Down Expand Up @@ -62,6 +63,65 @@ class ScrollableSegmentedControlTests: XCTestCase {
XCTAssert(collectionView?.numberOfItems(inSection: 0) == 3)
}

func testSelectedIndex(){
segmentedControl.insertSegment(withTitle: "segment 1", image: nil, at: 0)
segmentedControl.insertSegment(withTitle: "segment 2", image: nil, at: 1)
segmentedControl.insertSegment(withTitle: "segment 3", image: nil, at: 2)

segmentedControl.underlineSelected = true
segmentedControl.selectedSegmentIndex = 1

XCTAssert(segmentedControl.selectedSegmentIndex == 1)

let collectionView = segmentedControl.viewWithTag(1) as? UICollectionView
XCTAssertNotNil(collectionView)
let indexPath = collectionView!.indexPathsForSelectedItems?.last
XCTAssert(indexPath?.item == 1)
}

func testUnderlineIsPresent(){
segmentedControl.insertSegment(withTitle: "segment 1", image: nil, at: 0)
segmentedControl.insertSegment(withTitle: "segment 2", image: nil, at: 1)
segmentedControl.insertSegment(withTitle: "segment 3", image: nil, at: 2)

let collectionView = segmentedControl.viewWithTag(1) as? UICollectionView
XCTAssertNotNil(collectionView)

segmentedControl.underlineSelected = true
segmentedControl.selectedSegmentIndex = 1


let indexPath = collectionView!.indexPathsForSelectedItems?.last
XCTAssertNotNil(indexPath)
let cell = collectionView?.dataSource?.collectionView(collectionView!, cellForItemAt: indexPath!)
XCTAssertNotNil(cell)

let underlineView = cell?.contentView.viewWithTag(999)
XCTAssertNotNil(underlineView)
}

func testTintColor(){
segmentedControl.insertSegment(withTitle: "segment 1", image: nil, at: 0)
segmentedControl.insertSegment(withTitle: "segment 2", image: nil, at: 1)
segmentedControl.insertSegment(withTitle: "segment 3", image: nil, at: 2)

let collectionView = segmentedControl.viewWithTag(1) as? UICollectionView
segmentedControl.underlineSelected = true
segmentedControl.selectedSegmentIndex = 1

let indexPath = collectionView!.indexPathsForSelectedItems?.last
var cell = collectionView?.dataSource?.collectionView(collectionView!, cellForItemAt: indexPath!)
var underlineView = cell?.contentView.viewWithTag(999)

let color = UIColor.purple
XCTAssertFalse(underlineView?.backgroundColor == color)

segmentedControl.tintColor = color
cell = collectionView?.dataSource?.collectionView(collectionView!, cellForItemAt: indexPath!)
underlineView = cell?.contentView.viewWithTag(999)
XCTAssertTrue(underlineView?.backgroundColor == color)
}

// func testPerformanceExample() {
// // This is an example of a performance test case.
// self.measure {
Expand Down

0 comments on commit dbaccbb

Please sign in to comment.