Created
September 14, 2017 19:15
-
-
Save kfarst/9f8a1eb59cce2004b15f0b682c92eeed to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: Playground - noun: a place where people can play | |
import UIKit | |
import PlaygroundSupport | |
class Responder: NSObject { | |
@objc func segmentedControlValueChanged(_ sender: UISegmentedControl) { | |
UIView.animate(withDuration: 0.3) { | |
buttonBar.frame.origin.x = (segmentedControl.frame.width / CGFloat(segmentedControl.numberOfSegments)) * CGFloat(segmentedControl.selectedSegmentIndex) | |
} | |
} | |
} | |
let responder = Responder() | |
// Container view | |
let view = UIView(frame: CGRect(x: 0, y: 0, width: 400, height: 100)) | |
view.backgroundColor = .white | |
let segmentedControl = UISegmentedControl() | |
// Add segments | |
segmentedControl.insertSegment(withTitle: "One", at: 0, animated: true) | |
segmentedControl.insertSegment(withTitle: "Two", at: 1, animated: true) | |
segmentedControl.insertSegment(withTitle: "Three", at: 2, animated: true) | |
// First segment is selected by default | |
segmentedControl.selectedSegmentIndex = 0 | |
segmentedControl.backgroundColor = .clear | |
segmentedControl.tintColor = .clear | |
segmentedControl.setTitleTextAttributes([ | |
NSAttributedStringKey.font : UIFont(name: "DINCondensed-Bold", size: 18), | |
NSAttributedStringKey.foregroundColor: UIColor.lightGray | |
], for: .normal) | |
segmentedControl.setTitleTextAttributes([ | |
NSAttributedStringKey.font : UIFont(name: "DINCondensed-Bold", size: 18), | |
NSAttributedStringKey.foregroundColor: UIColor.orange | |
], for: .selected) | |
// This needs to be false since we are using auto layout constraints | |
segmentedControl.translatesAutoresizingMaskIntoConstraints = false | |
let buttonBar = UIView() | |
// This needs to be false since we are using auto layout constraints | |
buttonBar.translatesAutoresizingMaskIntoConstraints = false | |
buttonBar.backgroundColor = UIColor.orange | |
// Add the segmented control to the container view | |
view.addSubview(segmentedControl) | |
view.addSubview(buttonBar) | |
// Constrain the segmented control to the top of the container view | |
segmentedControl.topAnchor.constraint(equalTo: view.topAnchor).isActive = true | |
// Constrain the segmented control width to be equal to the container view width | |
segmentedControl.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true | |
// Constrain the height of the segmented control to an arbitrarily chosen value | |
segmentedControl.heightAnchor.constraint(equalToConstant: 40).isActive = true | |
// Constrain the top of the button bar to the bottom of the segmented control | |
buttonBar.topAnchor.constraint(equalTo: segmentedControl.bottomAnchor).isActive = true | |
buttonBar.heightAnchor.constraint(equalToConstant: 5).isActive = true | |
// Constrain the button bar to the left side of the segmented control | |
buttonBar.leftAnchor.constraint(equalTo: segmentedControl.leftAnchor).isActive = true | |
// Constrain the button bar to the width of the segmented control divided by the number of segments | |
buttonBar.widthAnchor.constraint(equalTo: segmentedControl.widthAnchor, multiplier: 1 / CGFloat(segmentedControl.numberOfSegments)).isActive = true | |
segmentedControl.addTarget(responder, action: #selector(responder.segmentedControlValueChanged(_:)), for: UIControlEvents.valueChanged) | |
PlaygroundPage.current.liveView = view |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
More Improved version,
tabSegmentedControlChanged
Call back on Control Changed event,segmentItems
from outside via funcsetSegmentItems
setSelectedSegmentIndex
`import Foundation
import UIKit
class TabSegmentedControlView: UIView {
private let bottomBar: UIView = {
let view = UIView()
view.backgroundColor = #colorLiteral(red: 0.8819062114, green: 0.1067187563, blue: 0.2246125638, alpha: 1)
view.translatesAutoresizingMaskIntoConstraints = false
return view
}()
private var segmentItems: [String] = ["title","title"] {
didSet {
guard !segmentItems.isEmpty else { return }
setupSegmentItems()
bottomBarWidthAnchor?.isActive = false
bottomBarWidthAnchor = bottomBar.widthAnchor.constraint(equalTo: segmentControl.widthAnchor, multiplier: 1 / CGFloat(segmentItems.count))
bottomBarWidthAnchor?.isActive = true
}
}
var bottomBarWidthAnchor: NSLayoutConstraint?
}
`