Skip to content

Instantly share code, notes, and snippets.

@SAllen0400
Created February 7, 2017 02:40
Show Gist options
  • Save SAllen0400/802129bfe688fa35b0406916179b0000 to your computer and use it in GitHub Desktop.
Save SAllen0400/802129bfe688fa35b0406916179b0000 to your computer and use it in GitHub Desktop.
Custom Segmented Control
import Foundation
import UIKit
@IBDesignable class BMSegmentedControl: UIControl {
private var labels = [UILabel]()
var thumbView = UIView()
var items: [String] = ["BEFORE", "NEITHER", "AFTER"] {
didSet {
setupLabels()
}
}
var selectedIndex : Int = 0 {
didSet {
displayNewSelectedIndex()
}
}
@IBInspectable var selectedLabelColor : UIColor = UIColor.blackColor() {
didSet {
setSelectedColors()
}
}
@IBInspectable var unselectedLabelColor : UIColor = UIColor.whiteColor() {
didSet {
setSelectedColors()
}
}
@IBInspectable var thumbColor : UIColor = UIColor.whiteColor() {
didSet {
setSelectedColors()
}
}
@IBInspectable var borderColor : UIColor = UIColor.whiteColor() {
didSet {
layer.borderColor = borderColor.CGColor
}
}
@IBInspectable var font : UIFont! = UIFont.systemFontOfSize(12) {
didSet {
setFont()
}
}
override init(frame: CGRect) {
super.init(frame: frame)
setupSegmentedControl()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
setupSegmentedControl()
}
func setupSegmentedControl() {
setupLabels()
addIndividualItemConstraints(labels, mainView: self, padding: 0)
insertSubview(thumbView, atIndex: 0)
selectedIndex = 1
}
func setupLabels(){
for label in labels {
label.removeFromSuperview()
}
labels.removeAll(keepCapacity: true)
for index in 1...items.count {
let label = UILabel(frame: CGRectMake(0, 0, 70, 40))
label.text = items[index - 1]
label.backgroundColor = UIColor.clearColor()
label.textAlignment = .Center
label.font = UIFont(name: Fonts.avenirNextDemiBoldItalic, size: 12)
label.textColor = index == 1 ? selectedLabelColor : unselectedLabelColor
label.translatesAutoresizingMaskIntoConstraints = false
addSubview(label)
labels.append(label)
}
addIndividualItemConstraints(labels, mainView: self, padding: 0)
}
override func layoutSubviews() {
super.layoutSubviews()
var selectFrame = bounds
let newWidth = CGRectGetWidth(selectFrame) / CGFloat(items.count)
selectFrame.size.width = newWidth
thumbView.frame = selectFrame
thumbView.backgroundColor = thumbColor
thumbView.layer.cornerRadius = thumbView.frame.height / 2
layer.cornerRadius = bounds.height / 2
layer.borderColor = Colors.brightOrange.CGColor
layer.borderWidth = 2
backgroundColor = UIColor.clearColor()
displayNewSelectedIndex()
}
override func beginTrackingWithTouch(touch: UITouch, withEvent event: UIEvent?) -> Bool {
let location = touch.locationInView(self)
var calculatedIndex : Int?
for (index, item) in labels.enumerate() {
if item.frame.contains(location) {
calculatedIndex = index
}
}
if calculatedIndex != nil {
selectedIndex = calculatedIndex!
sendActionsForControlEvents(.ValueChanged)
}
return false
}
func displayNewSelectedIndex(){
for (_, item) in labels.enumerate() {
item.textColor = unselectedLabelColor
}
let label = labels[selectedIndex]
label.textColor = selectedLabelColor
UIView.animateWithDuration(0.65, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 14.0, options: .CurveEaseInOut, animations: {
self.thumbView.frame = label.frame
}, completion: nil)
}
func addIndividualItemConstraints(items: [UIView], mainView: UIView, padding: CGFloat) {
for (index, button) in items.enumerate() {
let topConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: mainView, attribute: NSLayoutAttribute.Top, multiplier: 1.0, constant: 0)
let bottomConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: mainView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 0)
var rightConstraint : NSLayoutConstraint!
if index == items.count - 1 {
rightConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: mainView, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: -padding)
}else{
let nextButton = items[index+1]
rightConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: nextButton, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: -padding)
}
var leftConstraint : NSLayoutConstraint!
if index == 0 {
leftConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: mainView, attribute: NSLayoutAttribute.Left, multiplier: 1.0, constant: padding)
}else{
let prevButton = items[index-1]
leftConstraint = NSLayoutConstraint(item: button, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: prevButton, attribute: NSLayoutAttribute.Right, multiplier: 1.0, constant: padding)
let firstItem = items[0]
let widthConstraint = NSLayoutConstraint(item: button, attribute: .Width, relatedBy: NSLayoutRelation.Equal, toItem: firstItem, attribute: .Width, multiplier: 1.0 , constant: 0)
mainView.addConstraint(widthConstraint)
}
mainView.addConstraints([topConstraint, bottomConstraint, rightConstraint, leftConstraint])
}
}
func setSelectedColors(){
for item in labels {
item.textColor = unselectedLabelColor
}
if labels.count > 0 {
labels[0].textColor = selectedLabelColor
}
thumbView.backgroundColor = thumbColor
}
func setFont(){
for item in labels {
item.font = font
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment