Changing the Range of a UISlider Programmatically

2019-06-25 13:42发布

问题:

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?

回答1:

Here is it.

UISlider* slider = [[UISlider alloc] init];
slider.minimumValue = -3.0f;
slider.maximumValue = 3.0f;


回答2:

// 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];


回答3:

In the case updating selected values of IBOutlet-ed RangeSeekSlider

rangeSeekSlider.selectedMinValue = 10.0
rangeSeekSlider.selectedMaxValue = 100.0
rangeSeekSlider.setNeedsLayout()