WBSegmentControl 0.2.3

WBSegmentControl 0.2.3

TestsTested
LangLanguage SwiftSwift
License MIT
ReleasedLast Release Apr 2017
SwiftSwift Version 3.0
SPMSupports SPM

Maintained by Wonder Bear.



  • By
  • xiongxiong

WBSegmentControl

An easy to use, customizable segment control, can be used to show tabs.

WBSegmentControl

Contents

Features

  • [x] Flexible style - rainbow | cover | strip | arrow | arrowStrip
  • [x] Action delegate support
  • [x] Customized segment

Requirements

  • iOS 8.0+ / Mac OS X 10.11+ / tvOS 9.0+
  • Xcode 8.0+
  • Swift 3.0+

Installation

Manually

If you prefer not to use either of the aforementioned dependency managers, you can integrate WBSegmentControl into your project manually.

Example

Open the example project, build and run.

Protocols

WBSegmentControlDelegate

public protocol WBSegmentControlDelegate {
    func segmentControl(segmentControl: WBSegmentControl, selectIndex newIndex: Int, oldIndex: Int)
}

WBSegmentProtocol

public protocol WBSegmentContentProtocol {
    var type: WBSegmentType { get }
}

Usage

Implement WBSegmentContentProtocol

class TextSegment: NSObject, WBSegmentContentProtocol {

    var text: String!

    var type: WBSegmentType {
        return WBSegmentType.Text(text)
    }

    init(text: String) {
        super.init()

        self.text = text
    }
}

IconSegment & TextSegment are already implemented, you can use it straightforwardly without implementing WBSegmentContentProtocol, or you can choose to implement WBSegmentContentProtocol to use your own segment type.

Initialize segmentControl

let segmentControl = WBSegmentControl() // initialize
view.addSubview(segmentControl)
...
segmentControl.segments = [
    TextSegment(text: "News China"),
    TextSegment(text: "Breaking News"),
] // set segments
segmentControl.style = .Rainbow // set style
segmentControl.selectedIndex = 0 // set selected index

Implement WBSegmentControlDelegate

extension MyViewController: WBSegmentControlDelegate {
    func segmentControl(segmentControl: WBSegmentControl, selectIndex newIndex: Int, oldIndex: Int) {

    }
}

Get selected segment

let selectedIndex = segmentControl.selectedIndex
let selectedSegment: TextSegment? = segmentControl.selectedSegment as? TextSegment

Properties

Settings - Common

  • indicatorStyle: IndicatorStyle = rainbow | cover | strip | arrow | arrowStrip
  • nonScrollDistributionStyle: NonScrollDistributionStyle = center | left | right | average
  • enableSeparator: Bool
  • separatorColor: UIColor
  • separatorWidth: CGFloat
  • separatorEdgeInsets: UIEdgeInsets
  • enableSlideway: Bool
  • slidewayHeight: CGFloat
  • slidewayColor: UIColor
  • enableAnimation: Bool
  • animationDuration: NSTimeInterval
  • contentBackgroundColor: UIColor
  • segmentMinWidth: CGFloat
  • segmentEdgeInsets: UIEdgeInsets
  • segmentTextBold: Bool
  • segmentTextFontSize: CGFloat
  • segmentTextForegroundColor: UIColor
  • segmentTextForegroundColorSelected: UIColor

Settings - indicatorStyle == .Rainbow

  • rainbow_colors: [UIColor]
  • rainbow_height: CGFloat
  • rainbow_roundCornerRadius: CGFloat
  • rainbow_location: RainbowLocation = up | down
  • rainbow_outsideColor: UIColor

Settings - indicatorStyle == .Cover

  • cover_range: CoverRange = segment | content
  • cover_opacity: Float
  • cover_color: UIColor

Settings - indicatorStyle == .Strip

  • strip_range: StripRange = segment | content
  • strip_location: StripLocation = up | down
  • strip_color: UIColor
  • strip_height: CGFloat

Settings - indicatorStyle == .Rainbow

public var rainbow_colors: [UIColor] = []
public var rainbow_height: CGFloat = 3
public var rainbow_roundCornerRadius: CGFloat = 4
public var rainbow_location: RainbowLocation = .Down
public var rainbow_outsideColor: UIColor = UIColor.grayColor()

Settings - indicatorStyle == .Arrow

  • arrow_size: CGSize
  • arrow_location: ArrowLocation = up | down
  • arrow_color: UIColor

Settings - indicatorStyle == .ArrowStrip

  • arrowStrip_location: ArrowStripLocation = up | down
  • arrowStrip_color: UIColor
  • arrowStrip_arrowSize: CGSize
  • arrowStrip_stripHeight: CGFloat
  • arrowStrip_stripRange: ArrowStripRange = segment | content

Author

xiongxiong, [email protected]

License

WBSegmentControl is available under the MIT license. See the LICENSE file for more info.