Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I create and display a UISlider programmatically in iOS?

Tags:

iphone

ios4

I found the code below in the Apple documentation and added it to my viewDidLoad method but the slider doesn't appear when I run the code.

CGRect frame = CGRectMake(0.0, 0.0, 200.0, 10.0); UISlider *slider = [[UISlider alloc] initWithFrame:frame]; [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged]; [slider setBackgroundColor:[UIColor clearColor]]; slider.minimumValue = 0.0; slider.maximumValue = 50.0; slider.continuous = YES; slider.value = 25.0; 
like image 396
Christian Gossain Avatar asked Oct 23 '10 22:10

Christian Gossain


People also ask

How do you use sliders in Swift?

Enter Swift as Language and choose Next. Go to the Storyboard and drag a Slider to the main view and span its width to the width of the main View. Select the slider and go to the Attributes inspector. Under the slider section change the maximum value to 100 and change the current value to 0.

Can you set the current value of the slider beyond the range min max?

If you try to programmatically set a slider's current value to be below the minimum or above the maximum, it's set to the minimum or maximum instead. However, if you set the value beyond the range of the minimum or maximum in Interface Builder, the minimum or minimum values are updated instead.


2 Answers

You need to add the slider into your view hierarchy. Add [self.view addSubview:slider]; and it should work.

like image 80
Michal Avatar answered Sep 28 '22 02:09

Michal


Try this Code:

Create a new slider

-(IBAction)mySlider {      CGRect frame = CGRectMake(0.0, 0.0, 200.0, 10.0);     UISlider *slider = [[UISlider alloc] initWithFrame:frame];     [slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];     [slider setBackgroundColor:[UIColor clearColor]];     slider.minimumValue = 0.0;     slider.maximumValue = 50.0;     slider.continuous = YES;     slider.value = 25.0;     [self.view addSubview:slider]; } 

Slider Actions

-(void)sliderAction:(id)sender {     UISlider *slider = (UISlider*)sender;     float value = slider.value;     //-- Do further actions } 
like image 21
Rajesh Loganathan Avatar answered Sep 28 '22 01:09

Rajesh Loganathan