SSegmentControl
Simple SegmentControl for custom UIView Segments. Segments can be passed as UIViews, so theres no UI limitations for the single segments.
Example
To run the example project, clone the repo, and run pod install
from the Example directory first.
Requirements
- Swift 5.0
- SnapKit 5.0
Installation
SSegmentControl is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod 'SSegmentControl'
Basic usage
// Define views to pass it as segments to segmentControl
let followerCount: CounterSegment = {
let view = CounterSegment()
view.count = 1000
view.title = "Follower"
return view
}()
let followingCount: CounterSegment = {
let view = CounterSegment()
view.count = 500
view.title = "Following"
return view
}()
let label: UILabel = {
let label: UILabel = UILabel()
label.font = UIFont.boldSystemFont(ofSize: 15.0)
label.textColor = .black
label.textAlignment = .center
label.text = "Posts"
return label
}()
// Create segmentControl and pass the created views to it.
lazy var segmentControl: SSegmentControl = {
let view: SSegmentControl = SSegmentControl(segments: [
self.label,
self.followerCount,
self.followingCount
])
// Configure appearance of segmentControl
view.selectorColor = .red
view.isShadowHidden = true
return view
}()
// Add change listener closure anywhere in your ViewController
self.segmentControl.segmentDidChanged = { index, view in
// React on changes
self.contentLabel.text = "Selected segment: \(index)"
}
Other Options
Actions
segmentControl.move(to: 0) // Move segment control to passed index
Listener
segmentDidChange: (Int, UIView) -> Void // Called when segment did changed
Getter
segmentControl.selectedViewIndex // Returns index of selected view
You can customize these properties of the segment control:
selectorColor
: Color of the selctor view. Default value isUIColor.red
segmentsBackgroundColor
: Background color of the segments container view. Default value is.white
isShadowHidden
: Indicates if SegmentControl should throw a shadow. Default value istrue
Author
alexanderkorus, [email protected]
License
SSegmentControl is available under the MIT license. See the LICENSE file for more info.