4

How is it possible to set the min max ranges of the UISlider programmatically?

For example (dummy code)

UISlider* slider = [[UISlider alloc] init];
slider.min = -3;
slider.max = 3;

EDIT:

So I have the following:

sl.minimumValue = 5;
NSLog(@"MIN VAL: %d", sl.minimumValue);

This doesn't work, I still get it logging the value 0. Is this because I have set values in interface builder?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user559142
  • 12,279
  • 49
  • 116
  • 179
  • i advice to you always set ANY values by code. also remember `sl.minimumValue` is float. `NSLog(@"MIN VAL: %f", sl.minimumValue);` that should work. – sherilyn Aug 11 '11 at 19:29
  • I'm sorry, I don't understand your reply. I create the slider in interface builder but I can;t set its values programatically... – user559142 Aug 11 '11 at 19:30
  • just try to change code to this `sl.minimumValue = 5.0f; NSLog(@"MIN VAL: %f", sl.minimumValue);`. also start app with brackpoint in this part of code. – sherilyn Aug 11 '11 at 19:33
  • I cannot get it to work, it seems IB overrides my code...I am placing this in the viewDidLoad method – user559142 Aug 11 '11 at 19:50
  • it can't be. xib loads on init method. viewDidLoad is calling after. did you connect your slider to code in IB? – sherilyn Aug 11 '11 at 20:04

4 Answers4

10

Here is it.

UISlider* slider = [[UISlider alloc] init];
slider.minimumValue = -3.0f;
slider.maximumValue = 3.0f;
sherilyn
  • 374
  • 2
  • 10
  • this doesn't work. it is loading the min max values from interface builder...how do i stop it from doing that – user559142 Aug 11 '11 at 19:29
2

In the case updating selected values of IBOutlet-ed RangeSeekSlider

rangeSeekSlider.selectedMinValue = 10.0
rangeSeekSlider.selectedMaxValue = 100.0
rangeSeekSlider.setNeedsLayout()
ak_ninan
  • 721
  • 7
  • 15
0
// Add a frame where you want to place the slider. This will place it at (x,y) = 0,0     
with a height of 10 and width of 200

CGRect frame = CGRectMake(0.0, 0.0, 200.0, 10.0);

// sliderAction will respond to the updated slider value
UISlider *slider = [[UISlider alloc] initWithFrame:frame];
[slider addTarget:self action:@selector(sliderAction:) forControlEvents:UIControlEventValueChanged];

// Set minimum and maximum value
slider.minimumValue = 0.0;
slider.maximumValue = 50.0;
slider.continuous = YES;

// Initial value
slider.value = 25.0;

// Add slider to view
[self.view addSubview:slider];
chwi
  • 2,752
  • 2
  • 41
  • 66
0

Swift 5 :

self.yourSlider.minimumValue = 0.0
self.yourSlider.maximumValue = 4
self.yourSlider.value = 0           
Ekramul Hoque
  • 672
  • 4
  • 17