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