UISegmentedControl change number of segments programmatically

IosUikitUisegmentedcontrol

Ios Problem Overview


Is there a way to change the number of segments programmatically?

Ios Solutions


Solution 1 - Ios

Yes, you can use

removeSegmentAtIndex:(NSUInteger) animated:(BOOL)

And

insertSegmentWithTitle:(NSString *) atIndex:(NSUInteger) animated:(BOOL)

Solution 2 - Ios

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.

Solution 3 - Ios

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)
        }
    }
}

Solution 4 - Ios

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)
		}
	}
}

Solution 5 - Ios

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:

enter image description here

Solution 6 - Ios

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)

Solution 7 - Ios

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)
  }
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBeppino66View Question on Stackoverflow
Solution 1 - IosScarView Answer on Stackoverflow
Solution 2 - IosZorayrView Answer on Stackoverflow
Solution 3 - IoskernelpanicView Answer on Stackoverflow
Solution 4 - Iosuser7828620View Answer on Stackoverflow
Solution 5 - Iossam_smithView Answer on Stackoverflow
Solution 6 - IosPablo RuanView Answer on Stackoverflow
Solution 7 - IosFaizal Nowshad KNView Answer on Stackoverflow