Skip to content

Commit e849465

Browse files
committed
added badge counts for segments
1 parent 19cfbe9 commit e849465

File tree

5 files changed

+57
-4
lines changed

5 files changed

+57
-4
lines changed

Example/LGSegmentedControl.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@
390390
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
391391
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
392392
MODULE_NAME = ExampleApp;
393-
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.LGSegmentedControl-Example-9";
393+
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.LGSegmentedControl-Example";
394394
PRODUCT_NAME = "$(TARGET_NAME)";
395395
SWIFT_VERSION = 4.2;
396396
};
@@ -406,7 +406,7 @@
406406
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
407407
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
408408
MODULE_NAME = ExampleApp;
409-
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.LGSegmentedControl-Example-9";
409+
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.LGSegmentedControl-Example";
410410
PRODUCT_NAME = "$(TARGET_NAME)";
411411
SWIFT_VERSION = 4.2;
412412
};

Example/LGSegmentedControl/ViewController.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ class ViewController: UIViewController {
3636
]
3737
// make sure to set the selected index AFTER assigning the segments
3838
segmentedControl.selectedIndex = 1
39+
40+
segmentedControl.segments.first?.badgeCount = 3
3941
}
4042

4143
@IBAction func selectedSegment(_ segmentedControl: LGSegmentedControl) {

LGSegmentedControl/Classes/Extensions.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,9 @@ public func ???<T>(optional: T?, defaultValue: @autoclosure () -> String) -> Str
7676
case nil: return defaultValue()
7777
}
7878
}
79+
80+
extension Int {
81+
var string: String {
82+
return "\(self)"
83+
}
84+
}

LGSegmentedControl/Classes/LGSegment.swift

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class LGSegment {
1313
var contentView: UIView = {
1414
let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
1515
view.backgroundColor = .clear
16+
view.layer.zPosition = 0
1617
return view
1718
}()
1819
var titleLabel: UILabel = {
@@ -26,15 +27,32 @@ class LGSegment {
2627
let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
2728
view.clipsToBounds = true
2829
view.translatesAutoresizingMaskIntoConstraints = false
30+
view.layer.zPosition = 1
2931
return view
3032
}()
31-
33+
lazy var badgeView: UIView = {
34+
let view = UIView(frame: CGRect(x: 0, y: 0, width: 16, height: 16))
35+
view.backgroundColor = options.badgeColor.background
36+
view.translatesAutoresizingMaskIntoConstraints = false
37+
view.layer.zPosition = 2
38+
return view
39+
}()
40+
lazy var badgeLabel: UILabel = {
41+
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 16, height: 16))
42+
label.textColor = options.badgeColor.text
43+
label.font = UIFont.systemFont(ofSize: 13, weight: .medium)
44+
label.textAlignment = .center
45+
label.sizeToFit()
46+
label.translatesAutoresizingMaskIntoConstraints = false
47+
return label
48+
}()
3249
lazy var tapView: UIView = {
3350
let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
3451
view.backgroundColor = .clear
3552
view.translatesAutoresizingMaskIntoConstraints = false
3653
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))
3754
view.addGestureRecognizer(tap)
55+
view.layer.zPosition = 3
3856
return view
3957
}()
4058

@@ -51,10 +69,17 @@ class LGSegment {
5169

5270
var options = LGSegmentOptions()
5371

72+
public var badgeCount: Int? {
73+
didSet {
74+
updateAppearance(with: options)
75+
}
76+
}
77+
5478
var delegate: LGSegmentDelegate?
5579

56-
public init(title: String) {
80+
public init(title: String, badgeCount: Int? = nil) {
5781
self.title = title
82+
self.badgeCount = badgeCount
5883

5984
updateAppearance(with: options, animated: false)
6085

@@ -63,6 +88,8 @@ class LGSegment {
6388

6489
private func setupConstraints() {
6590
backgroundView.addSubview(titleLabel)
91+
badgeView.addSubview(badgeLabel)
92+
contentView.addSubview(badgeView)
6693
contentView.addSubview(backgroundView)
6794
contentView.addSubview(tapView)
6895

@@ -78,6 +105,19 @@ class LGSegment {
78105
titleLabel.topAnchor.constraint(greaterThanOrEqualTo: backgroundView.topAnchor, constant: 6).isActive = true
79106
titleLabel.bottomAnchor.constraint(greaterThanOrEqualTo: backgroundView.bottomAnchor, constant: 6).isActive = true
80107

108+
badgeLabel.centerYAnchor .constraint(equalTo: badgeView.centerYAnchor).isActive = true
109+
badgeLabel.leadingAnchor .constraint(equalTo: badgeView.leadingAnchor , constant: 4).isActive = true
110+
badgeLabel.trailingAnchor.constraint(equalTo: badgeView.trailingAnchor, constant: -4).isActive = true
111+
badgeLabel.topAnchor .constraint(equalTo: badgeView.topAnchor, constant: 0.5).isActive = true
112+
badgeLabel.bottomAnchor .constraint(equalTo: badgeView.bottomAnchor, constant:-0.5).isActive = true
113+
114+
badgeView.leadingAnchor .constraint(equalTo: titleLabel.trailingAnchor, constant: 0).isActive = true
115+
badgeView.bottomAnchor .constraint(equalTo: titleLabel.topAnchor , constant: 6).isActive = true
116+
let badgeViewSize: CGFloat = 16
117+
badgeView.heightAnchor .constraint(greaterThanOrEqualToConstant: badgeViewSize).isActive = true
118+
badgeView.widthAnchor .constraint(greaterThanOrEqualToConstant: badgeViewSize).isActive = true
119+
badgeView.layer.cornerRadius = badgeViewSize/2
120+
81121
tapView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
82122
tapView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
83123
tapView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
@@ -95,7 +135,9 @@ class LGSegment {
95135
// others
96136
self.backgroundView.layer.cornerRadius = options.cornerRadius
97137
self.titleLabel.font = options.font
138+
self.badgeView.isHidden = self.badgeCount == nil
98139
}
140+
badgeLabel.text = badgeCount?.string ?? ""
99141
}
100142

101143
@objc private func handleTap() {

LGSegmentedControl/Classes/LGSegmentOptions.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,8 @@ class LGSegmentOptions {
2424
/// Font of the segments' title labels
2525
var font: UIFont = UIFont.systemFont(ofSize: 15)
2626

27+
/// Background and text color of the segments' badges
28+
var badgeColor: (background: UIColor, text: UIColor) = (.red, .white)
29+
2730
init() { }
2831
}

0 commit comments

Comments
 (0)