Search code examples
iosuislider

UISlider setMaximumTrackTintColor in iOS 7.1


[slider setMaximumTrackTintColor: color]

has unexpected results in iOS 7.1 (the slider bar changes its position appearing at top instead of vertical center or disappears completely), while working fine with prior versions.

[slider setMinimumTrackTintColor: color]

does render the expected result.

This question might be related: UISlider setMaximumTrackTintColor, but no answer so far.

Update:

I get this: wrong instead of: enter image description here

Update #2:

Using setMaximumTrackImage might work, but the solution I'm looking for is a way to set any random color and not a preexisting image.

Update #3:

This issue is still present in iOS 7.1.1.


Solution

  • Found this workaroud:

    Create a 1x1px UIImage from a UIColor on the fly:

    CGRect rect = CGRectMake(0, 0, 1, 1);
    UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
    [color setFill];
    UIRectFill(rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    and then

    [slider setMaximumTrackImage:image forState:UIControlStateNormal];
    

    Looks like an expensive solution but it gets the job done.