UISlider setMaximumTrackTintColor

2019-02-08 09:29发布

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).

6条回答
劳资没心,怎么记你
2楼-- · 2019-02-08 10:10

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.

查看更多
放荡不羁爱自由
3楼-- · 2019-02-08 10:17

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.

查看更多
劫难
4楼-- · 2019-02-08 10:21

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.

查看更多
Melony?
5楼-- · 2019-02-08 10:33

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.
}

Slider with 2 different colors

查看更多
霸刀☆藐视天下
6楼-- · 2019-02-08 10:33

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.

查看更多
贪生不怕死
7楼-- · 2019-02-08 10:34

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

查看更多
登录 后发表回答