Android style segmented control written in swift. Fully customisable.
Swift Ruby
Switch branches/tags
Nothing to show
Clone or download
nahitheper Merge pull request #17 from stockx/jrmsklar/use-attributed-strings
Use attributes to configure the selected and unselected styles of titles
Latest commit a674421 Aug 6, 2017

README.md

YSSegmentedControl

Android style segmented control written in swift.
Fully customisable.

Demo

alt tag

Install

Manual

Copy & paste YSSegmentedControl.swift in your project

CocoaPods
use_frameworks!
pod 'YSSegmentedControl'

Usage

Create YSSegmentedControl with frame and titles.
You can either use delegation or callback initilization

With callback
    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)")
        })
With delegation
    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

YSSegmentedControlDelegate

@objc protocol YSSegmentedControlDelegate {
    optional func segmentedControlWillPressItemAtIndex (segmentedControl: YSSegmentedControl, index: Int)
    optional func segmentedControlDidPressedItemAtIndex (segmentedControl: YSSegmentedControl, index: Int)
}

YSSegmentedControlAppearance

	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 = ...