iosswiftswift3uisegmentedcontrol

Fill programmatically UISegmentedControl using swift


Is possible fill the values of a UISegmentedControl programmatically using swift?


Solution

  • Do it like this:

    let segmentedControl = UISegmentedControl()
    segmentedControl.insertSegment(withTitle: "Title", at: 0, animated: true)
    segmentedControl.setTitle("Another Title", forSegmentAt: 0)
    

    Remember that inserting segments and updating the title should always be done on the main thread. The same goes for adding it to a view if it's not already added to one.