Is there a way to change the number of segments programmatically?
Yes, you can use
removeSegmentAtIndex:(NSUInteger) animated:(BOOL)
And
insertSegmentWithTitle:(NSString *) atIndex:(NSUInteger) animated:(BOOL)
To replace the segments entirely, you can use the following function:
- (void)setSegments:(NSArray *)segments
{
[segmentController removeAllSegments];
for (NSString *segment in segments) {
[segmentController insertSegmentWithTitle:segment atIndex:segmentController.numberOfSegments animated:NO];
}
}
Hope this helps.
And here's a little Swift extension to replace current segmentedControl with array of new values
extension UISegmentedControl {
func replaceSegments(segments: Array<String>) {
self.removeAllSegments()
for segment in segments {
self.insertSegmentWithTitle(segment, atIndex: self.numberOfSegments, animated: false)
}
}
}
extension UISegmentedControl {
func replaceSegments(segments: Array<String>) {
self.removeAllSegments()
for segment in segments {
self.insertSegment(withTitle: segment, at: self.numberOfSegments, animated: false)
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With