I\'m trying to dynamically configure the track color on a UISlider.
This line of code works perfectly for setting the low side of the slider track.
[
That is strange, both min and max work fine for me and I can even have it animate color change. All I can suggest is re-create the slider and also @synthesize.
@synthesize slider;
- (void)viewDidLoad
{
[super viewDidLoad];
[slider setMinimumTrackTintColor:[UIColor orangeColor]];
[slider setMaximumTrackTintColor:[UIColor blueColor]];
// Do any additional setup after loading the view, typically from a nib.
}
I believe that this is an Apple problem. There are lots of people reporting strange UISlider behavior with 7.1. I personally can't get the minimum tint color to change. I think there's nothing you can do at this point.
Avoid setting the maximumTrackTintColor redundantly. When my code was updating the color twice, the maximum line would just disappear. I was able to reproduce it with a simple UISlider, like this:
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 150, 23)];
slider.maximumTrackTintColor = [UIColor whiteColor];
slider.maximumTrackTintColor = [UIColor whiteColor];
Instead of a white track, this slider will have no maximum track when displayed on the screen under iOS 8.
For me the problem occurred because I had subclassed UISlider
to create a custom trackRectForBounds
that was returning a CGRect with an invalid size. My original implementation (and maybe some iOS version's implementations?) returned a track bounds with a zero or negative size depending on the UISlider's bounds. It appears that it is related to the fact that setting minimum
/maximumTrackTintColor
will call trackRectForBounds
, possibly in order to create a track image.
The fix to the problem is making sure that the track bounds will be valid when setting minimum/maximum track tint colors.
Option 1: use initWithFrame that is sufficiently sized for the UISlider
UISlider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0,0,100,38)];
slider.minimumTrackTintColor = [UIColor greenColor];
slider.maximumTrackTintColor = [UIColor orangeColor];
// ... rest of view setup ...
Option 2: add the slider and force auto-layout so that it is correctly sized before setting the tint colors
UISlider *slider = [[UISlider alloc] init]; // <-- problem, zero size
[view addSubview:slider];
// ... other view setup, including constraints ...
[view layoutIfNeeded]; // forced layout to give slider a non-zero size
slider.minimumTrackTintColor = [UIColor greenColor];
slider.maximumTrackTintColor = [UIColor orangeColor];
Option 3: fix the override to make sure that it will return a rectangle of non-zero size, regardless of bounds.
- (CGRect)trackRectForBounds:(CGRect)bounds {
// example, inset left/right by 2 (thus width - 4)
// track height is 5 pixels, centered in bounds.
return CGRectMake(
CGRectGetMinX(bounds) + 2,
CGRectGetMinY(bounds) + (CGRectGetHeight(bounds) + 5)/2.0,
MAX(10, CGRectGetWidth(bounds) - 4), // use max make sure positive.
5)
}
Note: I am creating UISliders after the viewDidLoad
so
Ahufford's answer wouldn't work for me, but I presume this could also explain it. I would also guess Ahufford's answer will probably work best in most cases.
IOS 8.3
It's works with theses methods :
[slider setMinimumTrackTintColor:[UIColor orangeColor]];
[slider setMaximumTrackTintColor:[UIColor blueColor]];
OR :
slider.minimumTrackTintColor = [UIColor orangeColor];
slider.maximumTrackTintColor = [UIColor blueColor];
EDIT
Sorry, I had to be wrong during my tests ... But I can not change maximumTrackTintColor via Interface Builder....
Use above methods if you want change maximumTrackTintColor of UISlider
This is an Apple bug, and it still exists in iOS 8. I've seen it when trying to modify the maximum tint color of sliders via an proxy setter on a subclass. Changing the minimum tint color works fine and shows no errors, but trying to change the maximum tint color throws tons of bad context errors and often draws that portion of the slider gradient in the wrong location (which proves it does indeed not have a good context).
I could find no workaround for the problem, but will file a bug report.