iOS - UISlider 的自定义图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11348225/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:09:25  来源:igfitidea点击:

iOS - custom image for UISlider

ioscustomizationuislider

提问by soleil

I want to use an image for the UISlider track. I don't want one color on the left of the thumb, and another color on the right. I just want one static image across the whole track. Possible?

我想为 UISlider 轨道使用图像。我不希望拇指左侧有一种颜色,而右侧有另一种颜色。我只想要一张贯穿整个赛道的静态图像。可能的?

回答by Midhun MP

For setting the image to your slider you can use the setMinimumTrackImage, setMaximumTrackImagemethods. For your requirement set both to same image.

要将图像设置为滑块,您可以使用setMinimumTrackImagesetMaximumTrackImage方法。根据您的要求,将两者设置为相同的图像。

iOS 5 and Below

iOS 5 及以下

UIImage *sliderTrackImage = [[UIImage imageNamed: @"Slider.png"] stretchableImageWithLeftCapWidth: 7 topCapHeight: 0];

[mySlider setMinimumTrackImage: sliderTrackImage forState: UIControlStateNormal];
[mySlider setMaximumTrackImage: sliderTrackImage forState: UIControlStateNormal];

iOS 5+

iOS 5+

UIImage *sliderTrackImage = [[UIImage imageNamed:@"Slider.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(0, 7, 0, 0)];

[mySlider setMinimumTrackImage: sliderTrackImage forState: UIControlStateNormal];
[mySlider setMaximumTrackImage: sliderTrackImage forState: UIControlStateNormal];

For more please check these links:

有关更多信息,请查看这些链接:

  1. User Interface Customisation Tutorial
  2. http://jasonlawton.com/blog/customizing-uislider-in-iphone/
  3. Custom UISlider
  4. Slider image
  1. 用户界面自定义教程
  2. http://jasonlawton.com/blog/customizing-uislider-in-iphone/
  3. 自定义 UISlider
  4. 滑块图像

回答by Darshit Shah

[[UISlider appearance] setThumbImage:[UIImage imageNamed:@"ball.png"] forState:UIControlStateNormal];
[slider setMinimumTrackImage:[[UIImage imageNamed:@"volume_slider_oragne.png"] stretchableImageWithLeftCapWidth:0.3 topCapHeight:0.0] forState:UIControlStateNormal];
[slider setMaximumTrackImage:[[UIImage imageNamed:@"volume_strap_gry.png"] stretchableImageWithLeftCapWidth:0.3 topCapHeight:0.0] forState:UIControlStateNormal];

回答by Eric Welander

Just set both sides to the same image. You might want to make two separate images with the same color/pattern if you want the rounded corners on the ends.

只需将两侧设置为相同的图像。如果您想要两端的圆角,您可能需要制作两个具有相同颜色/图案的单独图像。