UISlider setMaximumTrackTintColor in iOS 7.1

2019-01-17 17:04发布

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

标签: ios uislider
5条回答
乱世女痞
2楼-- · 2019-01-17 17:17

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.

查看更多
ゆ 、 Hurt°
3楼-- · 2019-01-17 17:18

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

查看更多
做自己的国王
4楼-- · 2019-01-17 17:24

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.

查看更多
Emotional °昔
5楼-- · 2019-01-17 17:29

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.

查看更多
神经病院院长
6楼-- · 2019-01-17 17:37

Update

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

查看更多
登录 后发表回答