Is possible fill the values of a UISegmentedControl programmatically using swift?
let segmentedControl = UISegmentedControl()
segmentedControl.insertSegment(withTitle: "Title", at: 0, animated: true)
segmentedControl.setTitle("Another Title", forSegmentAt: 0)
                        If I am not mistaking, you mean that you want to add segments to "UISegmentedControl" component programmatically, without using the Interface Builder.
Yes, it is possible:
// Assuming that it is an "IBOutlet", you can do this in your "ViewController":
class ViewController: UIViewController {
    @IBOutlet weak var segmentedControl: UISegmentedControl!
    override func viewDidLoad() {
        super.viewDidLoad()
        // remove all current segments to make sure it is empty:
        segmentedControl.removeAllSegments()
        // adding your segments, using the "for" loop is just for demonstration:
        for index in 0...3 {
           segmentedControl.insertSegmentWithTitle("Segment \(index + 1)", atIndex: index, animated: false)
        }
        // you can also remove a segment like this:
        // this removes the second segment "Segment 2"
        segmentedControl.removeSegmentAtIndex(1, animated: false)
    }
    // and this is how you can access the changing of its value (make sure that event is "Value Changed")
    @IBAction func segmentControlValueChanged(sender: UISegmentedControl) {
        print("index of selected segment is: \(sender.selectedSegmentIndex)")
    }
}
                        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