xpepermint
xpepermint

Reputation: 36253

iPhone: UISlider dynamically change maximumValue

UISlider's scrub is not repositioned when I change its maximumValue property. I dynamically change the maximumValue property of the UISlider B by scrubbing UISlider A. So when the UISlider A is changed for e.g. 1 UISlider B has maximumValue+=1. UISlider B should auto updatet its its scrub position but it does not. If I then press on UISlider B scrub it gets updated.

What is the solution for that?

Upvotes: 1

Views: 3118

Answers (2)

Steve HHH
Steve HHH

Reputation: 13147

I had this exact problem: the UISlider thumb does not reposition itself on the track when maximumValue is set programmatically, even if you explicitly call setValue. For example:

// Setup:
UISlider *a = [[UISlider alloc] init];
a.minimumValue = 1.00;
a.maximumValue = 100.00;
[a setValue: 50.00];

/* CORRECT: The thumb is in the centre of the track, at position "50.00":

1 --------------------------- () --------------------------- 100
                         a.value == 50
*/ 

// Set maximumValue programmatically:
[a setMaximumValue: 1000.00];

/* INCORRECT: The thumb is still in the centre of the track: position "500.00":

1 --------------------------- () --------------------------- 1000
                         a.value == 50
    |<---- It should be here (!)
*/ 

// Call setValue explicitly, to no avail:
[a setValue: 50.00];

/* INCORRECT: The thumb is still in the centre of the track: position "500.00":

1 --------------------------- () --------------------------- 1000
                         a.value == 50
    |<---- It should be here (!)
*/ 

The only way I was able to solve this (after hours of looking for a solution online, in books, and experimenting) was to change the UISlider's value, which seems to force the UISlider to redraw. I don't like it, but it works:

[a setValue:a.value+1 animated:NO];
[a setValue:a.value-1 animated:NO];

/* (The thumb is now at position "50.00"):

1 -- () ---------------------------------------------------- 1000
  a.value == 50
*/

If anyone knows a better way to do this, I would love to read about it. (-:

Upvotes: 3

JFoulkes
JFoulkes

Reputation: 2429

If you want to change the value of the slider thumb you need to use:

- (void)setValue:(float)value animated:(BOOL)animated

maximumValue only changes the upper value of what the thumb can represent

Upvotes: 0

Related Questions