UISlider thumbTintColor doesn't change on iOS

2019-02-16 07:51发布

I have an app that runs perfectly on iOS 6. I've set a blinking effect to a UISlider's thumb this way:

-(void)startBlinkingSlider{
    isSliderBlinking = YES;
    isSliderTinted = NO;
    [self performSelector:@selector(toggleSliderColor) withObject:nil afterDelay:0.2];
}

-(void)toggleSliderColor{
    if(isSliderBlinking){
        if(isSliderTinted){
            self.effectAmountSlider.thumbTintColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
        }else{
            self.effectAmountSlider.thumbTintColor = [UIColor colorWithRed:255 green:0 blue:0 alpha:1];
        }
        isSliderTinted = !isSliderTinted;
        [self performSelector:@selector(toggleSliderColor) withObject:nil afterDelay:0.2];
    }
}

-(void)stopBlinkingSlider{
    isSliderBlinking = NO;
    isSliderTinted = NO;
    self.effectAmountSlider.thumbTintColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1];
}

When I call startBlinkingSlider my slider starts blinking red in iOS 6. If I run the same exact app on my iOS 7 device, nothing happens. The slider's thumb retains its original white color. I've set a breakpoint on the line where I set the thumbTintColor. In debugger, here is what I'm getting:

(lldb) po self.effectAmountSlider.thumbTintColor
error: failed to get API lock
(lldb) po self.effectAmountSlider.thumbTintColor
UIDeviceRGBColorSpace 0 0 0 1
(lldb) 

I typed the exact same code and got a weird message in the first one. However, the second result is correct. Then after setting it to red I'm also getting the correct result:

(lldb) po self.effectAmountSlider.thumbTintColor
UIDeviceRGBColorSpace 1 0 0 1

Even though the debugger shows the correct value, I'm getting no visual change in the slider. It's still white, color doesn't change in any way. I've searched Apple's documents here: https://developer.apple.com/library/ios/documentation/userexperience/conceptual/TransitionGuide/Controls.html

It doesn't say anything about UISlider's thumbTintColor not working as iOS 6. It should stay working as expected. I've checked the thread and everything is running on the main thread. toggleSliderColor is always on the main thread so it's not a threading issue. Why is my thumb color not working?

Thanks, Can.

4条回答
可以哭但决不认输i
2楼-- · 2019-02-16 08:22

I just so happened to read the iOS 7 UI Transition Guide again this morning, and tripped on a statement under Slider. If EITHER maximumTrackTineColor OR thumbTintColor are nil, then both properties are ignored. So I tried to set all the tint colors, thumb still white.

I entered a bug report on this - #15277127 - reference it if you enter your own bug. The more bug reports the more likely Apple will fix it soon.

EDIT: Apple duped my bug to another one - this was obviously known a while ago.

查看更多
狗以群分
3楼-- · 2019-02-16 08:26

On basis of @aaronsti's answer I found that the following worked for me. Setting thumb-image to nil had no effect.

  [_slider setThumbImage:[_slider thumbImageForState:UIControlStateNormal] forState:UIControlStateNormal];
_slider.minimumTrackTintColor = minTintColor;
_slider.thumbTintColor = thumbTintColor;
查看更多
太酷不给撩
4楼-- · 2019-02-16 08:40

So far in Xcode 6.2, iOS 8.2 the problem is still there, and the workaround still works. Swift version:

slider.setThumbImage(slider.thumbImageForState(.Normal), forState:.Normal)
查看更多
何必那么认真
5楼-- · 2019-02-16 08:42

I discovered a workaround. By first calling the 'setThumbImage:forState:' method, the 'thumbTintColor' property will then take effect.

[self.slider setThumbImage:[UIImage imageNamed:@"Thumb.png"] 
                  forState:UIControlStateNormal];
self.slider.thumbTintColor = [UIColor blackColor];

I tested this on Version 7.0 (463.9.4.2) of iOS Simulator.

查看更多
登录 后发表回答