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

有帮助吗?

解决方案

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.

其他提示

Try this out:

UIImage *sliderLeftTrackImage = [[UIImage imageNamed:@"LeftImage.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
UIImage *sliderRightTrackImage = [[UIImage imageNamed:@"RightImage.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];

[sliderName setMinimumTrackImage: sliderLeftTrackImage forState: UIControlStateNormal];
[sliderName setMaximumTrackImage: sliderRightTrackImage forState: UIControlStateNormal];

If you want to change thumb image the following code will work:

[sliderName setThumbImage:[UIImage imageNamed:@"ThumbImgName.png"] forState:UIControlStateNormal];
[sliderName setThumbImage:[UIImage imageNamed:@"ThumbImgName.png"] forState:UIControlStateHighlighted];

In same way, you can also use color instead of images.

I've created 2px images with the colour of slider track.

enter image description here

And then I set they as tracking images (here's with thumb image for iPad and iPhone)

UIImage *thumbImage = [UIImage imageNamed:@"slider"];
UIImage *trackImage;

if (isiPad) {
    [[UISlider appearance] setThumbImage:thumbImage forState:UIControlStateNormal];
    [[UISlider appearance] setThumbImage:thumbImage forState:UIControlStateHighlighted];
    trackImage = [UIImage imageNamed:@"menu-bar-ipad"];
}
else {
    [[UISlider appearance] setThumbImage:[self imageWithImage:thumbImage scaledToSize:CGSizeMake(27, 27)] forState:UIControlStateNormal];
    [[UISlider appearance] setThumbImage:[self imageWithImage:thumbImage scaledToSize:CGSizeMake(27, 27)] forState:UIControlStateHighlighted];
    trackImage = [UIImage imageNamed:@"menu-bar"];
}

[[UISlider appearance]  setMinimumTrackImage:trackImage forState:UIControlStateNormal];
[[UISlider appearance]  setMaximumTrackImage:trackImage forState:UIControlStateNormal];

And that's all. Same solution as msmq I guess. And you can see both ways how to make a large image - two images way and scaling way.

Update

Reported this bug and it's already well known. Hopefully they will fix it soon...

Maybe a little bit late, but - here is the answer

You should read description of setter:

The color used to tint the standard maximum track images. Setting this property removes any custom maximum track images associated with the slider.

Also this applicable only for maximum color, because minimum setter just change titntColor:

The color used to tint the standard minimum track images.

This mean that slider use some image for maximum track and if u just set tint color nothing will be changes (nothing can't be tinted).

enter image description here

Solution (thanks to @user623396):

UIImage *currentSliderMaximumImage = self.slider.currentMaximumTrackImage;

CGRect rect = CGRectMake(0, 0, currentSliderMaximumImage.size.width, currentSliderMaximumImage.size.height);
UIGraphicsBeginImageContextWithOptions(rect.size, NO, 0);
[[[DynamicUIService service] currentApplicationColor] setFill];
UIRectFill(rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self.slider setMaximumTrackImage:image forState:UIControlStateNormal];

As result u will get

enter image description here

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top