Change iPhone Uislider Bar Image

Change iPhone UISlider bar image

You were right to use -setMinimumTrackImage:forState: and -setMaximumTrackImage:forState: methods. What you missed is that you should provide stretchable UIImage to them, the rest is taken care of automagically:

UIImage *sliderLeftTrackImage = [[UIImage imageNamed: @"SliderMin.png"] stretchableImageWithLeftCapWidth: 9 topCapHeight: 0];
UIImage *sliderRightTrackImage = [[UIImage imageNamed: @"SliderMax.png"] stretchableImageWithLeftCapWidth: 9 topCapHeight: 0];
[mySlider setMinimumTrackImage: sliderLeftTrackImage forState: UIControlStateNormal];
[mySlider setMaximumTrackImage: sliderRightTrackImage forState: UIControlStateNormal];

You can use the same image for both the min and max parts.

Changing Background Image of UISlider?

1. Add image view with same dimension below your slider. Set your multicolor image to it.

2. Use transparent image as left and right of slider image

   UIImage *clearImage = [[UIImage imageNamed:@"clearSliderBar.png"] stretchableImageWithLeftCapWidth:14.0 topCapHeight:0.0];
[slider setMinimumTrackImage:clearImage forState:UIControlStateNormal];
[slider setMaximumTrackImage:clearImage forState:UIControlStateNormal];

This is 20x8 sample transparet image for test

iOS - custom image for UISlider

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

Customizing UISlider look

I believe you'll have to write your own slider to do that. There seems to be no (public) API to change UISlider's behavior regarding the stretchable region.

Change the position of thumb image on the slider bar

You can customize UISlider and override ThumbRectForBounds method. The method Returns the drawing rectangle for the slider’s thumb image.

Code :

public partial class ViewController1 : UIViewController 
{
public ViewController1() : base("ViewController1", null)
{
}

public override void ViewDidLoad()
{
base.ViewDidLoad();

MySlider slider = new MySlider();
slider.Frame = new CGRect(50, 50, 100, 20);
View.Add(slider);
}
}


public class MySlider : UISlider
{
public MySlider()
{
this.SetThumbImage(UIImage.FromFile("1.png"), UIControlState.Normal);
}

public override CGRect ThumbRectForBounds(CGRect bounds, CGRect trackRect, float value)
{
CGRect rect = base.ThumbRectForBounds(bounds, trackRect, value);
return new CGRect(rect.X, -10, rect.Width, rect.Height);
}
}

Sample Image

How to change image in UIImageView with UISlider in Swift

do like

@IBAction func valueChanged1(_ sender: UISlider) {

var imageName : String = "Image.png"

switch (sender.value)
{
case 0:
print("zero")
imageName = "yyyy.png"

case 1:
print("one")
imageName = "zzzz.png"
case 2:
print("two")
imageName = "aaaa.png"

default:
print("Integer out of range")
}

if let image = UIImage(named:imageName) {
scaleImage.image = image
}

}

How to change the size of a UISlider thumb when seeking

Create a normal and larger thumb image with the sizes you want as PDF and add it to the .xcassets.

Then use the following lines of code:

let normal = UIImage(named: "thumbSmall")
slider.setThumbImage(normal, for: .normal)
let highlighted = UIImage(named: "thumbLarger")
slider.setThumbImage(highlighted, for: .highlighted)

Then during sliding the larger thumb image is shown.

A short test looks like this:

thumb size during sliging



Related Topics



Leave a reply



Submit