Android style segmented control written in swift.
Fully customisable.
Copy & paste YSSegmentedControl.swift
in your project
use_frameworks!
pod 'YSSegmentedControl'
Create YSSegmentedControl
with frame and titles.
You can either use delegation or callback initilization
let segmented = YSSegmentedControl(
frame: CGRect(
x: 0,
y: 64,
width: view.frame.size.width,
height: 44),
titles: [
"First",
"Second",
"Third"
],
action: {
control, index in
println ("segmented did pressed \(index)")
})
let segmented = YSSegmentedControl(
frame: CGRect(
x: 0,
y: 64,
width: view.frame.size.width,
height: 44),
titles: [
"First",
"Second",
"Third"
])
Setup the delegate and you are ready to go !
segmented.delegate = self
@objc protocol YSSegmentedControlDelegate {
optional func segmentedControlWillPressItemAtIndex (segmentedControl: YSSegmentedControl, index: Int)
optional func segmentedControlDidPressedItemAtIndex (segmentedControl: YSSegmentedControl, index: Int)
}
struct YSSegmentedControlAppearance {
var backgroundColor: UIColor
var selectedBackgroundColor: UIColor
var textColor: UIColor
var font: UIFont
var selectedTextColor: UIColor
var selectedFont: UIFont
var bottomLineColor: UIColor
var selectorColor: UIColor
var bottomLineHeight: CGFloat
var selectorHeight: CGFloat
}
The default appearance is
appearance = YSSegmentedControlAppearance(
backgroundColor: UIColor.clearColor(),
selectedBackgroundColor: UIColor.clearColor(),
textColor: UIColor.grayColor(),
font: UIFont.systemFontOfSize(15),
selectedTextColor: UIColor.blackColor(),
selectedFont: UIFont.systemFontOfSize(15),
bottomLineColor: UIColor.blackColor(),
selectorColor: UIColor.blackColor(),
bottomLineHeight: 0.5,
selectorHeight: 2)
You can change appearance by
segmented.appearance = YSSegmentedAppearance (...)
// or
segmented.appearance.titleColor = ...