Is there a way to change the number of segments programmatically?
Asked
Active
Viewed 5.4k times
8 Answers
142
Yes, you can use
removeSegmentAtIndex:(NSUInteger) animated:(BOOL)
And
insertSegmentWithTitle:(NSString *) atIndex:(NSUInteger) animated:(BOOL)

Scar
- 3,460
- 3
- 26
- 51
-
I solved with: [self.SegmentAnswer insertSegmentWithTitle:@"5" atIndex:4 animated:YES]; [self.SegmentAnswer insertSegmentWithTitle:@"6" atIndex:5 – Beppino66 Jul 24 '12 at 20:54
50
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.

Zorayr
- 23,770
- 8
- 136
- 129
-
3
-
1You can use [segmentController removeAllSegments] instead of the while loop. See the documentation at https://developer.apple.com/library/ios/documentation/UIKit/Reference/UISegmentedControl_Class/#//apple_ref/occ/instm/UISegmentedControl/removeAllSegments – MCR May 27 '15 at 16:29
-
-
Edited to use `[segmentController removeAllSegments] ` as per @MCR's comment. – Zorayr May 27 '15 at 17:22
37
And here's a little Swift extension to replace current segmentedControl with array of new values
Swift 3
extension UISegmentedControl {
func replaceSegments(segments: Array<String>) {
self.removeAllSegments()
for segment in segments {
self.insertSegmentWithTitle(segment, atIndex: self.numberOfSegments, animated: false)
}
}
}
Swift 4
extension UISegmentedControl {
func replaceSegments(segments: Array<String>) {
self.removeAllSegments()
for segment in segments {
self.insertSegment(withTitle: segment, at: self.numberOfSegments, animated: false)
}
}
}

Lucio
- 4,753
- 3
- 48
- 77

kernelpanic
- 2,876
- 3
- 34
- 58
5
Here is a Swift extension for replacing the segments with a sequence of strings. It’s similar to another answer given here except it can be used with any sequence, meaning you can also pass in slices, sets, etc.
extension UISegmentedControl {
/// Replace the current segments with new ones using a given sequence of string.
/// - parameter withTitles: The titles for the new segments.
public func replaceSegments<T: Sequence>(withTitles: T) where T.Iterator.Element == String {
removeAllSegments()
for title in withTitles {
insertSegment(withTitle: title, at: numberOfSegments, animated: false)
}
}
}

Community
- 1
- 1
-
1Can you add a link to the other answer that you're referring to? You can get a permalink to an answer by clicking on the "share" link under it. – Al Sweigart Apr 06 '17 at 19:17
3
For the sake of completeness (and because I ended up here looking for how to achieve the same thing in xib) here is how to do it in xib:

sam_smith
- 6,023
- 3
- 43
- 60
2
Simple than u think : Swift 5.7
func setSegmentedControl() {
// [1] remove all segments
segmentedControl.removeAllSegments()
// [2] insert your segments by your model
["title1", "title2", "title3"].enumerated().forEach { index, title in
segmentedControl.insertSegment(withTitle: title, at: index, animated: false)
}
// [3] optional - select default segment
segmentedControl.selectedSegmentIndex = 0
}

asilturk
- 198
- 1
- 7
0
work for me, UIsegmentedControll contains two segments, i want add one in index 2, use this code in swift 2.2 use:
SEG_TipoFiltro.insertSegmentWithTitle("Title", atIndex: 2, animated: false)

Pablo Ruan
- 1,681
- 1
- 17
- 12
0
adding and deleting segments in swift4 using code
class ViewController: UIViewController {
@IBOutlet weak var segment1: UISegmentedControl!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
@IBAction func insert(_ sender: Any) {
segment1.insertSegment(withTitle: "\(segment1.numberOfSegments+1)", at: segment1.numberOfSegments, animated: true)
}
@IBAction func remove(_ sender: Any) {
segment1.removeSegment(at: segment1.numberOfSegments-1, animated: true)
}
}

Piotr Labunski
- 1,638
- 4
- 19
- 26

Faizal Nowshad KN
- 99
- 5