I've customized my UISlider but my thumb image looks strange, I mean it's position not aligned by center:
But should be like this:
And here is code:
UIImage *leftTrack = [[UIImage imageNamed:#"blueTrack.png"] stretchableImageWithLeftCapWidth:3 topCapHeight:0];
UIImage *rightTrack = [[UIImage imageNamed:#"whiteTrack.png"] stretchableImageWithLeftCapWidth:3 topCapHeight:0];
[slider setThumbImage:[UIImage imageNamed:#"thumbButton.png"] forState:UIControlStateNormal];
[slider setMinimumTrackImage:leftTrack forState:UIControlStateNormal];
[slider setMaximumTrackImage:rightTrack forState:UIControlStateNormal];
There are two ways:
First one is to use
- (CGRect)trackRectForBounds:(CGRect)bounds;
and / or
- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value;
methods of UISlider when subclassing to lay out thumb the way you want
Second way is to adjust your artwork:
Related
I have to create my own customized slider. I am trying to change the thumb image,background of the slider and in the some test like slide to unlock.
I have tried like this but its not working
CGRect frame = CGRectMake(0.0, 219.0, 323.0, 20.0);
UISlider *myslider = [[UISlider alloc] initWithFrame:frame];
[myslider addTarget:self action:#selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
[myslider setBackgroundColor:[UIColor clearColor]];
[myslider setThumbImage: [UIImage imageNamed:#"sliderThumb#2x.png"] forState:UIControlStateNormal];
myslider.minimumValue = 0.0;
myslider.maximumValue = 50.0;
myslider.continuous = YES;
myslider.value = 15.0;
[self.view addSubview:myslider];
But in this the size of thumb pic is very big.How to reduce the thumb pic size....and how to give the slider background and text.Please help me out
[[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];
To set the background colour of the slider, I would presume that you can set the property backgroundColor to [UIColor colorWithPatternImage:[UIImage imageNamed:#"slider background"]];
Also, images that have the appending ending of #2x, mean that they are twice the size of the standard copy. This is for retina displays only, you should not use it in a UIImage on a standard (legacy) device. Try setting the image name to #"sliderThumb.png" instead, as long as you have the legacy copy of the image.
Just provide a different size of sliderThumb#2x.png and all we work itself out.
How can I create a slider in customized slider in iPhone. Currently I am using the slider selected from "xib"(Interface Builder) but it is showing me normal slider.But I need slider with customized with my own button and showing text
Slide To Start
How can I do this please help me out .
See following links
http://www.applausible.com/blog/?p=250
http://iphone4developer.blogspot.com/2010/12/custom-uislider.html
Change iPhone UISlider bar image
http://jainmarket.blogspot.com/2010/06/customize-default-uislider-in-iphone.html
http://www.xprogress.com/post-35-uislider-tutorial-example-how-to-use-slider-in-iphone-sdk-xcode/
http://www.obsessivecode.com/projects/ocprogress/
you can accomplish like
UISlider *mySlider;
mySlider = [[UISlider alloc]init];
/* Slider Customization */
[mySlider setThumbImage:[UIImage imageNamed:#"yourimage.png"] forState:UIControlStateNormal];
/*the above line will sets a custom image for your slider ball i.e; thumb image*/
/* the below code will make your slider line custom */
UIImage *sliderLeftTrackImage = [[UIImage imageNamed: #"lineImage.png"] stretchableImageWithLeftCapWidth: 9 topCapHeight: 0];
UIImage *sliderRightTrackImage = [[UIImage imageNamed: #"lineImage.png"] stretchableImageWithLeftCapWidth: 9 topCapHeight: 0];
[mySlider setMinimumTrackImage: sliderLeftTrackImage forState: UIControlStateNormal];
[mySlider setMaximumTrackImage: sliderRightTrackImage forState: UIControlStateNormal];
/* end of Slider Customization */
[mySlider release];
You will need to use stretchable images for the customization, and load them with the following methods:
setMinimumTrackImage:forState:
setMaximumTrackImage:forState:
setThumbImage:forState:
The minimum track image is the part on the left of the thumb (by default colored blue), and the maximum track image is on the right side.
Here is an example for the customization:
UIImage *minimumTrackImage = [UIImage imageNamed:#"sliderMin.png"];
UIImage *stretchableMinimumTrackImage = [minimumTrackImage stretchableImageWithLeftCapWidth:6 topCapHeight:6];
[slider setMinimumTrackImage:stretchableMinimumTrackImage forState:UIControlStateNormal];
UIImage *maximumTrackImage = [UIImage imageNamed:#"sliderMax.png"];
UIImage *stretchableMaximumTrackImage = [maximumTrackImage stretchableImageWithLeftCapWidth:6 topCapHeight:6];
[slider setMaximumTrackImage:stretchableMaximumTrackImage forState:UIControlStateNormal];
UIImage *handle = [UIImage imageNamed:#"sliderHandle.png"];
[slider setThumbImage:handle forState:UIControlStateNormal];
UISlider *objslider = [[UISlider alloc] initWithFrame:CGRectMake(14, 249, 290, 22)];
UIImage *stetchLeftTrack = [[UIImage imageNamed:#"slider_fill.png"] stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0];
UIImage *stetchRightTrack = [[UIImage imageNamed:#"slider.png"]
stretchableImageWithLeftCapWidth:0.0 topCapHeight:0.0];
[objslider setThumbImage: [UIImage imageNamed:#"slider_ball.png"]forState:UIControlStateNormal];
[objslider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
[objslider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
objslider.minimumValue = 0.0;
objslider.maximumValue = 100.0;
objslider.continuous = YES;
objslider.value = 0.0;
[self.view addSubview:objslider];
I'm implementing UISlider. I want to show images to the extreme end of the slider(eg. volume control images of mute and full sound). I'm using
UIImage *maximumValueImage = [UIImage imageNamed:#"speaker_plus.png"];
UIImage *minimumValueImage = [UIImage imageNamed:#"speaker_minus.png"];
slider.maximumValueImage = maximumValueImage;
slider.minimumValueImage = minimumValueImage;
How to implement the following method?
-(CGRect)maximumValueImageRectForBounds:(CGRect)bounds;
-(CGRect)minimumValueImageRectForBounds:(CGRect)bounds;
I did same for one of my app. Below is code.
UIImage *stetchLeftTrack = [[UIImage imageNamed:#"leftslide.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
UIImage *stetchRightTrack = [[UIImage imageNamed:#"rightslide.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
[slider setThumbImage:[UIImage imageNamed:#"imgSlider.png"] forState:UIControlStateNormal];
[slider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
[slider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
Hope this Help.
These are the images I used.
Black image is leftside.png
White image is rightside.png
Black Circle is imgSlider.png
Im my app, i need a slider with no bar initially. And as the user changes it, it should be visible.
As the user slides the thumb of the slider to the right the slider bar should start appearing on the left side.
Please share anything you have in your mind.
Thanks,
I've never tried this but I suppose it's possible. It sounds like a bad idea from a user-experience perspective, but that's outside the scope of this answer.
You can change the min and max images for a slider using the code below. You could add in some logic to hide these images initially (or set these images to transparent PNGs). Add a method called - (void)sliderAction:(id)sender which will be called when the slider is changed. Add logic there to change the images to something visible, or un-hide the images.
UIImage *minImage = [UIImage imageNamed:#"min.png"];
UIImage *maxImage = [UIImage imageNamed:#"max.png"];
UIImage *thumbImage= [UIImage imageNamed:#"thumb.png"];
minImage=[minImage stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
maxImage=[maxImage stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
UISlider* customSlider = [[UISlider alloc] initWithFrame:CGRectMake(30, 100, 200, 50)];
[customSlider setMinimumTrackImage:minImage forState:UIControlStateNormal];
[customSlider setMaximumTrackImage:maxImage forState:UIControlStateNormal];
[customSlider setThumbImage:thumbImage forState:UIControlStateNormal];
customSlider.minimumValue = 0.0;
customSlider.maximumValue = 100.0;
customSlider.continuous = YES;
customSlider.value = 50.0;
[customSlider addTarget:self action:#selector(sliderAction:) forControlEvents:UIControlEventValueChanged];
[self.view addSubview:customSlider];
[customSlider release];
I am having a button using IB. Now i want to add an image to the button programmatically. how can I set the button's size of image's size like we do in IB Layout -> Size to Fit . I want to do it programmatically
Thanks..
You can add an image to the button using UIButton's setImage:forState: method and then you set the content sizing using UIView's contentMode property.
An example would look like this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *img = [UIImage imageNamed:#"myImage.png"];
button.frame = CGRectMake(20, 100, img.size.width, img.size.height);
[button setImage:img forState:UIControlStateNormal];
[button setImage:img forState:UIControlStateHighlighted];
[button setImage:img forState:UIControlStateSelected];
button.contentMode = UIViewContentModeScaleToFill; //Look up UIViewContentMode in the documentation for other options
[self.view addSubview:button];
[yourButton setImage:yourImage forState:UIControlStateNormal];
yourButton.contentMode = UIViewContentModeScaleToFill;
where the the values for contentMode can be any one of the below
typedef enum {
UIViewContentModeScaleToFill,
UIViewContentModeScaleAspectFit, // contents scaled to fit with fixed aspect. remainder is transparent
UIViewContentModeScaleAspectFill, // contents scaled to fill with fixed aspect. some portion of content may be clipped.
UIViewContentModeRedraw, // redraw on bounds change (calls -setNeedsDisplay)
UIViewContentModeCenter, // contents remain same size. positioned adjusted.
UIViewContentModeTop,
UIViewContentModeBottom,
UIViewContentModeLeft,
UIViewContentModeRight,
UIViewContentModeTopLeft,
UIViewContentModeTopRight,
UIViewContentModeBottomLeft,
UIViewContentModeBottomRight,
} UIViewContentMode;
I think this could help you
Sample Code,
UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
UIImage *img1 = [UIImage imageNamed:#"image1.png"];
btn.frame = CGRectMake(20.0 , 270.0, img1.size.width, img1.size.height);
[btn setImage:img1 forState:UIControlStateNormal];
UIImage *img2 = [UIImage imageNamed:#"image2.png"];
[btn setImage:img2 forState:UIControlStateHighlighted];
[btn setImage:img2 forState:UIControlStateSelected];
[btn addTarget:self action:#selector(Action:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
Use the sizeToFit method of UIButton (UIView actually).