UISegmentedControl

UISegmentedControl.playground is an starting point to experiment with UISegmentedControl in an interactive playground:

UIView

import UIKit
import PlaygroundSupport

class SegmentsViewController : UIViewController {

    let items = ["A", "B", "C"]

    var label : UILabel!
    var segments : UISegmentedControl!

    override func loadView() {

        // UI

        let view = UIView()
        view.backgroundColor = .white

        label = UILabel()

        segments = UISegmentedControl(items: items)
        segments.addTarget(self, action: #selector(updateView), for: .valueChanged)

        view.addSubview(label)
        view.addSubview(segments)

        // Layout

        label.translatesAutoresizingMaskIntoConstraints = false
        segments.translatesAutoresizingMaskIntoConstraints = false
        NSLayoutConstraint.activate([
            segments.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            segments.topAnchor.constraint(equalTo: view.topAnchor, constant: 20),

            label.topAnchor.constraint(equalTo: segments.bottomAnchor, constant: 20),
            label.centerXAnchor.constraint(equalTo: segments.centerXAnchor),

        ])

        self.view = view

        updateView()
    }

    func updateView() {
        let idx = segments.selectedSegmentIndex
        let current = (idx == UISegmentedControlNoSegment) ? "none" : items[idx]
        label.text = "Current segment: \(current)"
    }

}


PlaygroundPage.current.liveView = SegmentsViewController()
Btn training bbbdf557d2 Next iOS training: 25. Februar - 01. März 2019, Stuttgart
Btn about 5378472193 About me · Contact