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];
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:
- User Interface Customisation Tutorial
- http://jasonlawton.com/blog/customizing-uislider-in-iphone/
- Custom UISlider
- 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);
}
}
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:
Related Topics
Which iOS App Version/Build Number(S) Must Be Incremented Upon App Store Release
How to Keep Uitableview Contentoffset After Calling -Reloaddata
Implement Pushkit and Test in Development Behavior
Passing Data to View Controllers That Are Embedded in Container Views
iOS UI Testing on an Isolated View
How to Access File Included in App Bundle in Swift
Generate Random Numbers with a Given Distribution
Swift - How Creating Custom Viewforheaderinsection, Using a Xib File
Swift 3 Core Data Delete Object
Building for iOS Simulator, But the Linked Framework '****.Framework' Was Built for iOS
Choosing the Right iOS Xml Parser
Creating Custom Info Window in Swift with the Google Maps iOS Sdk
Remove Grey Background on Link Clicked in iOS Safari/Chrome/Firefox
How to Get Touches When Parent View Has Userinteractionenabled Set to No in iOS