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.
[self.sliderBar setMinimumTrackTintColor:[UIColor redColor]];
When trying to do the same thing for the high side of the slider track, this line of code reports 3 fatal errors to the debug log.
[self.sliderBar setMaximumTrackTintColor:[UIColor redColor]];
<Error>: CGContextAddPath: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
<Error>: clip: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
<Error>: CGContextDrawLinearGradient: invalid context 0x0. This is a serious error. This application, or a library it uses, is using an invalid context and is thereby contributing to an overall degradation of system stability and reliability. This notice is a courtesy: please fix this problem. It will become a fatal error in an upcoming update.
I have also tried to set the track tint colors using dot notation, but the same 3 errors were reported.
self.sliderBar.maximumTrackTintColor = [UIColor redColor];
I am very confused why the minimum track tint color is properly set, while the maximum track tint color is breaking. Any assistance would be greatly appreciated.
This issue seems to be related to this question, but I'm not 100% sure since I'm not working with graphics (only setting the tint color).
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:
Instead of a white track, this slider will have no maximum track when displayed on the screen under iOS 8.
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.
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.
For me the problem occurred because I had subclassed
UISlider
to create a customtrackRectForBounds
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 settingminimum
/maximumTrackTintColor
will calltrackRectForBounds
, 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
Option 2: add the slider and force auto-layout so that it is correctly sized before setting the tint colors
Option 3: fix the override to make sure that it will return a rectangle of non-zero size, regardless of bounds.
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 :
OR :
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