I have a custom slider bar and when I slide it back and forth the track image seems to move left and right a little. Anyone know why this happens?
UIImage *stetchLeftTrack = [[UIImage imageNamed:highBar] stretchableImageWithLeftCapWidth:capLeft topCapHeight:0.0];
UIImage *stetchRightTrack = [[UIImage imageNamed:grooveBar] stretchableImageWithLeftCapWidth:capRight topCapHeight:0.0];
[customSlider setThumbImage: [UIImage imageNamed:thb] forState:UIControlStateNormal];
[customSlider setThumbImage: [UIImage imageNamed:thbH] forState:UIControlStateHighlighted];
[customSlider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
[customSlider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
Is it possible that your capLeft and capRight values are not what you expect (they should probably be nonzero integers)? It could be stretching the entire image rather than just the part you intend to have stretched.
Related
I have to place a slider and want to place an imge on slider. the slider i want is this
CAn any body help me?
See the UISlider class reference. You want to set setThumbImage:forState: as well as setMinimumTrackImage:forState: and setMaximumTrackImage:forState:. Use the same images for the minimum and maximum track images, and use UIControlStateNormal for the state.
Below code will be in ViewWillAppear...
-(void)viewWillAppear:(BOOL)animated
{
slider.backgroundColor = [UIColor clearColor];
UIImage *stetchLeftTrack = [[UIImage imageNamed:#"yellowslide.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
UIImage *stetchRightTrack = [[UIImage imageNamed:#"yellowslide.png"]
stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0];
[slider setThumbImage: [UIImage imageNamed:#"1.PNG"] forState:UIControlStateNormal];
[slider setMinimumTrackImage:stetchLeftTrack forState:UIControlStateNormal];
[slider setMaximumTrackImage:stetchRightTrack forState:UIControlStateNormal];
slider.minimumValue = 1.0;
slider.maximumValue = 3.0;
slider.continuous = YES;
slider.value = 0.0;
}
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).