Create UISlider Programmatically in Swift 4

UISlider control used to select a single value from a continuous range of value.



Initialization UISlider
override func viewDidLoad() {
    super.viewDidLoad()

    let slider = UISlider()
    slider.frame = CGRect(x: 0, y: 0, width: 250, height: 35)
    slider.center = self.view.center

    slider.minimumTrackTintColor = .green
    slider.maximumTrackTintColor = .red
    slider.thumbTintColor = .black

    slider.maximumValue = 100
    slider.minimumValue = 0
    slider.setValue(50, animated: false)

    slider.addTarget(self, action: #selector(ViewController.changeVlaue(_:)), for: .valueChanged)

    self.view.addSubview(slider)

}
Action Handler of UISlider 
@objc func changeVlaue(_ sender: UISlider) {
    print("value is" , Int(sender.value));
}

Output



Create UISlider Programmatically in Swift 4 Create UISlider Programmatically in Swift 4 Reviewed by KIRIT MODI on 07:01:00 Rating: 5

No comments:

Powered by Blogger.