Skip to content

Commit

Permalink
added badge counts for segments
Browse files Browse the repository at this point in the history
  • Loading branch information
LinusGeffarth committed Dec 31, 2018
1 parent 19cfbe9 commit e849465
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 4 deletions.
4 changes: 2 additions & 2 deletions Example/LGSegmentedControl.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.LGSegmentedControl-Example-9";
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.LGSegmentedControl-Example";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.2;
};
Expand All @@ -406,7 +406,7 @@
IPHONEOS_DEPLOYMENT_TARGET = 9.0;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
MODULE_NAME = ExampleApp;
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.LGSegmentedControl-Example-9";
PRODUCT_BUNDLE_IDENTIFIER = "org.cocoapods.demo.LGSegmentedControl-Example";
PRODUCT_NAME = "$(TARGET_NAME)";
SWIFT_VERSION = 4.2;
};
Expand Down
2 changes: 2 additions & 0 deletions Example/LGSegmentedControl/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ class ViewController: UIViewController {
]
// make sure to set the selected index AFTER assigning the segments
segmentedControl.selectedIndex = 1

segmentedControl.segments.first?.badgeCount = 3
}

@IBAction func selectedSegment(_ segmentedControl: LGSegmentedControl) {
Expand Down
6 changes: 6 additions & 0 deletions LGSegmentedControl/Classes/Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,9 @@ public func ???<T>(optional: T?, defaultValue: @autoclosure () -> String) -> Str
case nil: return defaultValue()
}
}

extension Int {
var string: String {
return "\(self)"
}
}
46 changes: 44 additions & 2 deletions LGSegmentedControl/Classes/LGSegment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class LGSegment {
var contentView: UIView = {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
view.backgroundColor = .clear
view.layer.zPosition = 0
return view
}()
var titleLabel: UILabel = {
Expand All @@ -26,15 +27,32 @@ class LGSegment {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
view.clipsToBounds = true
view.translatesAutoresizingMaskIntoConstraints = false
view.layer.zPosition = 1
return view
}()

lazy var badgeView: UIView = {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 16, height: 16))
view.backgroundColor = options.badgeColor.background
view.translatesAutoresizingMaskIntoConstraints = false
view.layer.zPosition = 2
return view
}()
lazy var badgeLabel: UILabel = {
let label = UILabel(frame: CGRect(x: 0, y: 0, width: 16, height: 16))
label.textColor = options.badgeColor.text
label.font = UIFont.systemFont(ofSize: 13, weight: .medium)
label.textAlignment = .center
label.sizeToFit()
label.translatesAutoresizingMaskIntoConstraints = false
return label
}()
lazy var tapView: UIView = {
let view = UIView(frame: CGRect(x: 0, y: 0, width: 100, height: 50))
view.backgroundColor = .clear
view.translatesAutoresizingMaskIntoConstraints = false
let tap = UITapGestureRecognizer(target: self, action: #selector(handleTap))
view.addGestureRecognizer(tap)
view.layer.zPosition = 3
return view
}()

Expand All @@ -51,10 +69,17 @@ class LGSegment {

var options = LGSegmentOptions()

public var badgeCount: Int? {
didSet {
updateAppearance(with: options)
}
}

var delegate: LGSegmentDelegate?

public init(title: String) {
public init(title: String, badgeCount: Int? = nil) {
self.title = title
self.badgeCount = badgeCount

updateAppearance(with: options, animated: false)

Expand All @@ -63,6 +88,8 @@ class LGSegment {

private func setupConstraints() {
backgroundView.addSubview(titleLabel)
badgeView.addSubview(badgeLabel)
contentView.addSubview(badgeView)
contentView.addSubview(backgroundView)
contentView.addSubview(tapView)

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

badgeLabel.centerYAnchor .constraint(equalTo: badgeView.centerYAnchor).isActive = true
badgeLabel.leadingAnchor .constraint(equalTo: badgeView.leadingAnchor , constant: 4).isActive = true
badgeLabel.trailingAnchor.constraint(equalTo: badgeView.trailingAnchor, constant: -4).isActive = true
badgeLabel.topAnchor .constraint(equalTo: badgeView.topAnchor, constant: 0.5).isActive = true
badgeLabel.bottomAnchor .constraint(equalTo: badgeView.bottomAnchor, constant:-0.5).isActive = true

badgeView.leadingAnchor .constraint(equalTo: titleLabel.trailingAnchor, constant: 0).isActive = true
badgeView.bottomAnchor .constraint(equalTo: titleLabel.topAnchor , constant: 6).isActive = true
let badgeViewSize: CGFloat = 16
badgeView.heightAnchor .constraint(greaterThanOrEqualToConstant: badgeViewSize).isActive = true
badgeView.widthAnchor .constraint(greaterThanOrEqualToConstant: badgeViewSize).isActive = true
badgeView.layer.cornerRadius = badgeViewSize/2

tapView.leadingAnchor.constraint(equalTo: contentView.leadingAnchor).isActive = true
tapView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor).isActive = true
tapView.topAnchor.constraint(equalTo: contentView.topAnchor).isActive = true
Expand All @@ -95,7 +135,9 @@ class LGSegment {
// others
self.backgroundView.layer.cornerRadius = options.cornerRadius
self.titleLabel.font = options.font
self.badgeView.isHidden = self.badgeCount == nil
}
badgeLabel.text = badgeCount?.string ?? ""
}

@objc private func handleTap() {
Expand Down
3 changes: 3 additions & 0 deletions LGSegmentedControl/Classes/LGSegmentOptions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@ class LGSegmentOptions {
/// Font of the segments' title labels
var font: UIFont = UIFont.systemFont(ofSize: 15)

/// Background and text color of the segments' badges
var badgeColor: (background: UIColor, text: UIColor) = (.red, .white)

init() { }
}

0 comments on commit e849465

Please sign in to comment.