As part of the visual improvements featured in iOS 13, UISegmentedControl
was completely redesigned, featuring a much rounder, cleaner, and slightly more skeuomorphic appearance.
TOSegmentedControl
is a subclass of of UIControl
that completely re-implements the look and feel of the new UISegmentedControl
component, allowing developers to adopt its look even in previous versions of iOS they support.
UISegmentedControl
, making it available on previous versions of iOS.@IBDesignable
and @IBInspectable
.UIControlEvents
to receive when the control is tapped.TOSegmentedControl
has been written to follow the interface of UISegmentedControl
as closely as possible. This should make it very intuitive to work with.
In Swift, the class is renamed to SegmentedControl
. Creating a new instance is very similar to UISegmentedControl
.
// Create a new instance
let segmentedControl = SegmentedControl(items: ["First", "Second", "Third"])
// Add a closure that will be called each time the selected segment changes
segmentedControlsegmentTappedHandler = { segmentIndex, reversed in
print("Segment \(segmentIndex) was tapped!")
}
// Add a new item to the end
segmentedControl.addSegment(withTitle: "Fourth")
// Insert a new item at the beginning
segmentedControl.insertSegment(withTitle: "Zero", at: 0)
// Remove all segments
segmentedControl.removeAllSegments()
NSArray *items = @[@"First", @"Second", @"Third"];
// Create a new instance
TOSegmentedControl *segmentedControl = [[TOSegmentedControl alloc] initWithItems:items]];
// Add a block that will be called each time the selected segment changes
segmentedControl.segmentTappedHandler = ^(NSInteger index, BOOL reversed) {
NSLog(@"Segment %d was tapped!", index);
};
// Add a new item to the end
[segmentedControl addSegmentWithTitle:@"Fourth"];
// Insert a new item at the beginning
[segmentedControl insertSegmentWithTitle:@"Zero" atIndex:0];
// Remove all segments
[segmentedControl removeAllSegments];
iOS 9.0 or above
TOSegmentedControl
was created by Tim Oliver as a component for iComics.
iOS device mockup by Pixeden.
TOSegmentedControl
is available under the MIT license. Please see the LICENSE file for more information.