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

Commit dbaccbb

Browse files
committed
Fixed issue with changing tintColor not been applied as underline (which marks selected segment) color.
1 parent b64cb14 commit dbaccbb

4 files changed

Lines changed: 77 additions & 5 deletions

File tree

Demo/ScrollableSegmentedControlDemo/ScrollableSegmentedControlDemo/Base.lproj/Main.storyboard

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<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">
2+
<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">
33
<device id="retina4_7" orientation="portrait">
44
<adaptation id="fullscreen"/>
55
</device>
66
<dependencies>
7-
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11524"/>
7+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11757"/>
88
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
99
</dependencies>
1010
<scenes>
@@ -17,9 +17,10 @@
1717
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
1818
<color key="backgroundColor" white="1" alpha="1" colorSpace="calibratedWhite"/>
1919
<view key="tableHeaderView" contentMode="scaleToFill" id="sFi-9r-jXX" customClass="ScrollableSegmentedControl" customModule="ScrollableSegmentedControl">
20-
<rect key="frame" x="0.0" y="64" width="375" height="49"/>
20+
<rect key="frame" x="0.0" y="0.0" width="375" height="49"/>
2121
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
2222
<color key="backgroundColor" red="0.90196079019999997" green="0.90196079019999997" blue="0.90196079019999997" alpha="1" colorSpace="calibratedRGB"/>
23+
<color key="tintColor" red="0.0" green="0.50196081400000003" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
2324
</view>
2425
<sections>
2526
<tableViewSection id="2bw-rH-tVL">

ScrollableSegmentedControl/Info.plist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<key>CFBundlePackageType</key>
1616
<string>FMWK</string>
1717
<key>CFBundleShortVersionString</key>
18-
<string>1.0.1</string>
18+
<string>1.0.2</string>
1919
<key>CFBundleVersion</key>
2020
<string>$(CURRENT_PROJECT_VERSION)</string>
2121
<key>NSPrincipalClass</key>

ScrollableSegmentedControl/ScrollableSegmentedControl.swift

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ public class ScrollableSegmentedControl: UIControl {
200200
flowLayout.minimumLineSpacing = 0
201201

202202
collectionView = UICollectionView(frame: frame, collectionViewLayout: flowLayout)
203+
collectionView!.tag = 1
203204
collectionView!.tintColor = tintColor
204205
collectionView!.register(TextOnlySegmentCollectionViewCell.self, forCellWithReuseIdentifier: CollectionViewController.textOnlyCellIdentifier)
205206
collectionViewController = CollectionViewController(segmentedControl: self)
@@ -320,6 +321,9 @@ public class ScrollableSegmentedControl: UIControl {
320321
}
321322

322323
segmentCell.showUnderline = segmentedControl.underlineSelected
324+
if segmentedControl.underlineSelected {
325+
segmentCell.tintColor = segmentedControl.tintColor
326+
}
323327

324328
return segmentCell
325329
}
@@ -350,6 +354,7 @@ public class ScrollableSegmentedControl: UIControl {
350354
underlineView?.removeFromSuperview()
351355
} else {
352356
underlineView = UIView()
357+
underlineView!.tag = 999
353358
underlineView!.backgroundColor = tintColor
354359
underlineView!.isHidden = !isSelected
355360
contentView.insertSubview(underlineView!, at: contentView.subviews.count)
@@ -360,6 +365,12 @@ public class ScrollableSegmentedControl: UIControl {
360365
}
361366
}
362367

368+
override var tintColor: UIColor!{
369+
didSet{
370+
underlineView?.backgroundColor = tintColor
371+
}
372+
}
373+
363374
override init(frame: CGRect) {
364375
super.init(frame: frame)
365376
configure()

ScrollableSegmentedControlTests/ScrollableSegmentedControlTests.swift

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ class ScrollableSegmentedControlTests: XCTestCase {
1414

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

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

66+
func testSelectedIndex(){
67+
segmentedControl.insertSegment(withTitle: "segment 1", image: nil, at: 0)
68+
segmentedControl.insertSegment(withTitle: "segment 2", image: nil, at: 1)
69+
segmentedControl.insertSegment(withTitle: "segment 3", image: nil, at: 2)
70+
71+
segmentedControl.underlineSelected = true
72+
segmentedControl.selectedSegmentIndex = 1
73+
74+
XCTAssert(segmentedControl.selectedSegmentIndex == 1)
75+
76+
let collectionView = segmentedControl.viewWithTag(1) as? UICollectionView
77+
XCTAssertNotNil(collectionView)
78+
let indexPath = collectionView!.indexPathsForSelectedItems?.last
79+
XCTAssert(indexPath?.item == 1)
80+
}
81+
82+
func testUnderlineIsPresent(){
83+
segmentedControl.insertSegment(withTitle: "segment 1", image: nil, at: 0)
84+
segmentedControl.insertSegment(withTitle: "segment 2", image: nil, at: 1)
85+
segmentedControl.insertSegment(withTitle: "segment 3", image: nil, at: 2)
86+
87+
let collectionView = segmentedControl.viewWithTag(1) as? UICollectionView
88+
XCTAssertNotNil(collectionView)
89+
90+
segmentedControl.underlineSelected = true
91+
segmentedControl.selectedSegmentIndex = 1
92+
93+
94+
let indexPath = collectionView!.indexPathsForSelectedItems?.last
95+
XCTAssertNotNil(indexPath)
96+
let cell = collectionView?.dataSource?.collectionView(collectionView!, cellForItemAt: indexPath!)
97+
XCTAssertNotNil(cell)
98+
99+
let underlineView = cell?.contentView.viewWithTag(999)
100+
XCTAssertNotNil(underlineView)
101+
}
102+
103+
func testTintColor(){
104+
segmentedControl.insertSegment(withTitle: "segment 1", image: nil, at: 0)
105+
segmentedControl.insertSegment(withTitle: "segment 2", image: nil, at: 1)
106+
segmentedControl.insertSegment(withTitle: "segment 3", image: nil, at: 2)
107+
108+
let collectionView = segmentedControl.viewWithTag(1) as? UICollectionView
109+
segmentedControl.underlineSelected = true
110+
segmentedControl.selectedSegmentIndex = 1
111+
112+
let indexPath = collectionView!.indexPathsForSelectedItems?.last
113+
var cell = collectionView?.dataSource?.collectionView(collectionView!, cellForItemAt: indexPath!)
114+
var underlineView = cell?.contentView.viewWithTag(999)
115+
116+
let color = UIColor.purple
117+
XCTAssertFalse(underlineView?.backgroundColor == color)
118+
119+
segmentedControl.tintColor = color
120+
cell = collectionView?.dataSource?.collectionView(collectionView!, cellForItemAt: indexPath!)
121+
underlineView = cell?.contentView.viewWithTag(999)
122+
XCTAssertTrue(underlineView?.backgroundColor == color)
123+
}
124+
65125
// func testPerformanceExample() {
66126
// // This is an example of a performance test case.
67127
// self.measure {

0 commit comments

Comments
 (0)