How To Create UI Slider In iOS (Swift 3 + Xcode 8) ?

How To Create UI Slider In iOS??

Hi, In this tutorial, I’ll show you how to use sliders in your iOS apps. Its super easy to learn and implement. So, without wasting any time, let’s start and see how to do it. :)

First of all, open your Xcode and create a new Single View Application. 



Give some name to your product and make sure to choose Swift as the language. Click Next.



Now, open Main.storyboard and drag 2 objects on your view controller. First drag a Slider (which is of course if you want to use sliders :| ) and then drag a label to display the values of this slider.

Now, add some constraints on both of these objects. For example, you can add following constraints on your label:



Similar way, add some constraints on the slider as well. You can add following constraints on your slider:



Also, don't forget to assign default, minimum and maximum values to your slider from attribute inspector as shown in the picture below:




Now, click on assistant editor from top right corner of your Xcode and create outlet for your label as shown in the picture below:






Similar way, create an action for your slider. Make sure to choose its type as UISlider (as shown in picture below) :




Once this done, open your ViewController.swift file and write just this one line of code in its Slider Action function.

 @IBAction func mySlider(_ sender: UISlider) {
        
        sliderValueLBL.text = String(Int(sender.value))

    }

Basically, we’re setting the text on our label here. but what text?? Its the value which we’re getting from our slider. First, we’re fetching the value of slider using sender.value and then we’re converting it to Int value so that we don’t get values like 50.12335 or 33.999 like that. And then we’re converting it to string value and saving it on our label.

Now you can run this app and play with your slider, it will change the value of your label whenever you move your slider’s pointer in any direction. 


Slider in iOS


Simple and easy, right?? 

I hope you enjoyed this tutorial. Let me know in the comments section below if you have any doubts or any questions related to sliders in your mind. I’ll try to respond you back with best of my knowledge. 

Thanks for your time. Keep coding..

Comments

Popular posts from this blog

How To Make The Device Vibrate Or Ring- An iOS Swift Tutorial

PayPal Integration In iOS Applications | Payment Gateways iOS/Swift

How To Parse JSON Data In iOS (Swift 3 + Xcode 8) ?