iOS - custom image for UISlider

2020-07-10 03:26发布

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?

3条回答
【Aperson】
2楼-- · 2020-07-10 03:33

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

iOS 5 and Below

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

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

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
查看更多
虎瘦雄心在
3楼-- · 2020-07-10 03:35

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.

查看更多
Luminary・发光体
4楼-- · 2020-07-10 03:58
[[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];
查看更多
登录 后发表回答