diff --git a/Demo/ScrollableSegmentedControlDemo/ScrollableSegmentedControlDemo.xcodeproj/xcshareddata/xcschemes/ScrollableSegmentedControlDemo.xcscheme b/Demo/ScrollableSegmentedControlDemo/ScrollableSegmentedControlDemo.xcodeproj/xcshareddata/xcschemes/ScrollableSegmentedControlDemo.xcscheme
new file mode 100644
index 0000000..e8174c4
--- /dev/null
+++ b/Demo/ScrollableSegmentedControlDemo/ScrollableSegmentedControlDemo.xcodeproj/xcshareddata/xcschemes/ScrollableSegmentedControlDemo.xcscheme
@@ -0,0 +1,91 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/Demo/ScrollableSegmentedControlDemo/ScrollableSegmentedControlDemo/Base.lproj/Main.storyboard b/Demo/ScrollableSegmentedControlDemo/ScrollableSegmentedControlDemo/Base.lproj/Main.storyboard
index 35d7bf1..77251ba 100644
--- a/Demo/ScrollableSegmentedControlDemo/ScrollableSegmentedControlDemo/Base.lproj/Main.storyboard
+++ b/Demo/ScrollableSegmentedControlDemo/ScrollableSegmentedControlDemo/Base.lproj/Main.storyboard
@@ -1,12 +1,11 @@
-
+
-
-
+
@@ -145,13 +144,13 @@
-
+
@@ -167,6 +166,39 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -213,7 +245,7 @@
-
+
diff --git a/Demo/ScrollableSegmentedControlDemo/ScrollableSegmentedControlDemo/TableViewController.swift b/Demo/ScrollableSegmentedControlDemo/ScrollableSegmentedControlDemo/TableViewController.swift
index 07f0dfa..dda5874 100644
--- a/Demo/ScrollableSegmentedControlDemo/ScrollableSegmentedControlDemo/TableViewController.swift
+++ b/Demo/ScrollableSegmentedControlDemo/ScrollableSegmentedControlDemo/TableViewController.swift
@@ -35,6 +35,7 @@ class TableViewController: UITableViewController {
segmentedControl.insertSegment(withTitle: "Seg 4", image: #imageLiteral(resourceName: "segment-4"), at: 3)
segmentedControl.insertSegment(withTitle: "Segment 5", image: #imageLiteral(resourceName: "segment-5"), at: 4)
segmentedControl.insertSegment(withTitle: "Segment 6", image: #imageLiteral(resourceName: "segment-6"), at: 5)
+ segmentedControl.underlineHeight = 3.0
segmentedControl.underlineSelected = true
segmentedControl.selectedSegmentIndex = 0
@@ -60,6 +61,9 @@ class TableViewController: UITableViewController {
}
}
+ @IBAction func underlineHeightChanged(_ sender: UISlider) {
+ segmentedControl.underlineHeight = CGFloat(sender.value)
+ }
@IBAction func toggleFixedWidth(_ sender: UISwitch) {
segmentedControl.fixedSegmentWidth = sender.isOn
segmentedControl.setNeedsLayout()
diff --git a/ScrollableSegmentedControl/ScrollableSegmentedControl.swift b/ScrollableSegmentedControl/ScrollableSegmentedControl.swift
index 7ca15a1..440ac46 100644
--- a/ScrollableSegmentedControl/ScrollableSegmentedControl.swift
+++ b/ScrollableSegmentedControl/ScrollableSegmentedControl.swift
@@ -97,6 +97,15 @@ public enum ScrollableSegmentedControlSegmentStyle: Int {
reloadSegments()
}
}
+
+ fileprivate var _underlineHeight: CGFloat = 4.0
+ @objc public dynamic var underlineHeight: CGFloat {
+ get { return _underlineHeight }
+ set {
+ _underlineHeight = newValue
+ reloadSegments()
+ }
+ }
fileprivate var _selectedSegmentContentColor:UIColor?
@objc public dynamic var selectedSegmentContentColor:UIColor? {
@@ -470,7 +479,7 @@ public enum ScrollableSegmentedControlSegmentStyle: Int {
segmentCell = cell
}
-
+ segmentCell.underlineHeight = segmentedControl.underlineHeight
segmentCell.showUnderline = segmentedControl.underlineSelected
if segmentedControl.underlineSelected {
segmentCell.tintColor = segmentedControl.tintColor
@@ -532,6 +541,11 @@ public enum ScrollableSegmentedControlSegmentStyle: Int {
static let defaultTextColor = UIColor.darkGray
var underlineView:UIView?
+ var underlineHeight: CGFloat = 4.0 {
+ didSet {
+ setNeedsUpdateConstraints()
+ }
+ }
public var contentColor:UIColor?
public var selectedContentColor:UIColor?
@@ -581,7 +595,9 @@ public enum ScrollableSegmentedControlSegmentStyle: Int {
private func configureConstraints() {
if let underline = underlineView {
underline.translatesAutoresizingMaskIntoConstraints = false
- underline.heightAnchor.constraint(equalToConstant: 4.0).isActive = true
+ let heightConstrain = underline.heightAnchor.constraint(equalToConstant: underlineHeight)
+ variableConstraints.append(heightConstrain)
+ heightConstrain.isActive = true
underline.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
underline.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
underline.bottomAnchor.constraint(equalTo: contentView.bottomAnchor).isActive = true
@@ -624,7 +640,11 @@ public enum ScrollableSegmentedControlSegmentStyle: Int {
override var isHighlighted: Bool {
didSet {
- if let title = (isHighlighted) ? super.highlightedAttributedTitle : super.normalAttributedTitle {
+ if isHighlighted, let title = super.highlightedAttributedTitle {
+ titleLabel.attributedText = title
+ } else if isSelected, let title = super.selectedAttributedTitle {
+ titleLabel.attributedText = title
+ } else if let title = super.normalAttributedTitle {
titleLabel.attributedText = title
} else {
titleLabel.isHighlighted = isHighlighted
@@ -668,7 +688,10 @@ public enum ScrollableSegmentedControlSegmentStyle: Int {
variableConstraints.append(titleLabel.centerYAnchor.constraint(equalTo: contentView.centerYAnchor))
variableConstraints.append(titleLabel.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: BaseSegmentCollectionViewCell.textPadding))
variableConstraints.append(titleLabel.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -BaseSegmentCollectionViewCell.textPadding))
-
+
+ if let underline = underlineView {
+ variableConstraints.append(underline.heightAnchor.constraint(equalToConstant: underlineHeight))
+ }
NSLayoutConstraint.activate(variableConstraints)
}
}
@@ -684,7 +707,7 @@ public enum ScrollableSegmentedControlSegmentStyle: Int {
override var isHighlighted: Bool {
didSet {
- if isHighlighted {
+ if isHighlighted || isSelected {
imageView.tintColor = (selectedContentColor == nil) ? BaseSegmentCollectionViewCell.defaultTextColor : selectedContentColor!
} else {
imageView.tintColor = (contentColor == nil) ? BaseSegmentCollectionViewCell.defaultTextColor : contentColor!
@@ -744,13 +767,17 @@ public enum ScrollableSegmentedControlSegmentStyle: Int {
override var isHighlighted: Bool {
didSet {
- if let title = (isHighlighted) ? super.highlightedAttributedTitle : super.normalAttributedTitle {
+ if isHighlighted, let title = super.highlightedAttributedTitle {
+ titleLabel.attributedText = title
+ } else if isSelected, let title = super.selectedAttributedTitle {
+ titleLabel.attributedText = title
+ } else if let title = super.normalAttributedTitle {
titleLabel.attributedText = title
} else {
titleLabel.isHighlighted = isHighlighted
}
- if isHighlighted {
+ if isHighlighted || isSelected {
imageView.tintColor = (selectedContentColor == nil) ? BaseSegmentCollectionViewCell.defaultTextColor : selectedContentColor!
} else {
imageView.tintColor = (contentColor == nil) ? BaseSegmentCollectionViewCell.defaultTextColor : contentColor!
diff --git a/ScrollableSegmentedControlTests/ScrollableSegmentedControlTests.swift b/ScrollableSegmentedControlTests/ScrollableSegmentedControlTests.swift
index c4f9d62..40e7f37 100644
--- a/ScrollableSegmentedControlTests/ScrollableSegmentedControlTests.swift
+++ b/ScrollableSegmentedControlTests/ScrollableSegmentedControlTests.swift
@@ -121,6 +121,32 @@ class ScrollableSegmentedControlTests: XCTestCase {
underlineView = cell?.contentView.viewWithTag(999)
XCTAssertTrue(underlineView?.backgroundColor == color)
}
+
+ func testUnderlineHeight() {
+ segmentedControl.insertSegment(withTitle: "segment 1", image: nil, at: 0)
+ segmentedControl.underlineSelected = true
+ segmentedControl.segmentStyle = .textOnly
+
+ self.segmentedControl.underlineHeight = 3
+ segmentedControl.selectedSegmentIndex = 0
+
+ var collectionView = self.segmentedControl.viewWithTag(1) as? UICollectionView
+ var indexPath = collectionView!.indexPathsForSelectedItems?.last
+ var cell = collectionView?.dataSource?.collectionView(collectionView!, cellForItemAt: indexPath!)
+ var underlineView = cell?.contentView.viewWithTag(999)
+ // The underline has only one constraint at its level, the height
+ XCTAssertTrue(underlineView?.constraints.first?.constant == 3)
+
+ self.segmentedControl.underlineHeight = 10
+
+ collectionView = self.segmentedControl.viewWithTag(1) as? UICollectionView
+ indexPath = collectionView!.indexPathsForSelectedItems?.last
+ cell = collectionView?.dataSource?.collectionView(collectionView!, cellForItemAt: indexPath!)
+ underlineView = cell?.contentView.viewWithTag(999)
+ // The underline has only one constraint at its level, the height
+ XCTAssertTrue(underlineView?.constraints.first?.constant == 10)
+
+ }
// func testPerformanceExample() {
// // This is an example of a performance test case.