How To Remove pre - Fill of UISlider - ios

I am having troubles with implementation if UISlider.
everything else is working fine except that when Slider thumb is at its minimum position it should not show minimum track image at all.
Screen shot of my iPad Simulator is as follows :-
in this picture, the thumb nob is at its minimum position but still it is showing "green minimum Track image" which it shouldn't
is there any way to come around this problem ?
code in my viewDidLoad method is :-
SLDR.continuous=NO;
[SLDR setMinimumTrackImage:[[UIImage imageNamed:#"U14.png"] stretchableImageWithLeftCapWidth:7 topCapHeight:0]forState:UIControlStateNormal];
[SLDR setMaximumTrackImage:[[UIImage imageNamed:#"U31.png"] stretchableImageWithLeftCapWidth:0 topCapHeight:0] forState:UIControlStateNormal];
[SLDR setThumbImage:[UIImage imageNamed:#"U9.png"] forState:UIControlStateNormal];
[SLDR setThumbImage:[UIImage imageNamed:#"U9.png"] forState:UIControlStateHighlighted];
Please tell me what am i missing, Thanks in advance :)

The standard slider thumbImage sits on top of the slider so it covers min positions. Yours is above so the end is exposed. Try creating a new image, by flipping your max image horizontally. Then add a valueCahnged to the slider like this...
- (IBAction)sliderValueChanged:(id)sender{
if (SLDR.value == 0) {
[SLDR setMinimumTrackImage:[[UIImage imageNamed:#"U31-flipped.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0]forState:UIControlStateNormal];
} else {
[SLDR setMinimumTrackImage:[[UIImage imageNamed:#"U14.png"] stretchableImageWithLeftCapWidth:5 topCapHeight:0]forState:UIControlStateNormal];
}
Hope this helps!

Related

How to design UIButton Like UITabBarItem in iOS?

I have to place 6 buttons in the bottom of particular screens like UITabBar. I have placed UIButton with Image and Text but, am not able to move the image on top of the button with centre alignment and place the button text in below of the image with center alignment. It should be look like UITabBarItem.
How can I achieve this in UIButton? Anyone can please help me on this? Thanks.
I tried this code but, text not visible and image not properly aligned. Can you please help me?
[self.languageButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
[self.languageButton setTitle:#"Language" forState:UIControlStateNormal];
[self.languageButton.titleLabel setFont:[UIFont boldSystemFontOfSize:10.0]];
UIImage *image = [UIImage imageNamed:#"lang_change_white"];
[self.languageButton setTitleEdgeInsets:UIEdgeInsetsMake(0.0, -image.size.width, -25.0, 0.0)];
[self.languageButton setImage:image forState:UIControlStateNormal];
[self.languageButton setImageEdgeInsets:UIEdgeInsetsMake(-15.0, 0.0, 0.0, -self.languageButton.titleLabel.bounds.size.width)];
Thanks in advance.
your code work fine for me then what is problem ?
plz explein what you want with image and text of the button..?
you have to make 2 special image which contains symbol and text. one image is white image and second one is any color of image . and when user press button set background image as your color image and set initial image as white color image.

How to reduce UISlider frame height?

It turns out that UISlider has a lower limit for its frame height. On iOS 6 it's 23px and on iOS 7it's 34 px. But that's too much for me and i need to make it smaller. How can i do that without using CGAffineTransformMakeScale (i tried that already and don't like how it looks)? Or maybe i'm just missing something really simple?
There's no properties you could use to do that.
I did find a github project which allows you set the the height of the bar.
Or have a look at the results on Cocoa Controls for 'Slider'.
Assign height constraint to slider in storyboard.
You have to use images of smaller size :
[slider setThumbImage:[UIImage imageNamed:#"Thumb"] forState:UIControlStateNormal]; // slider thumb should be square image
[slider setThumbImage:[UIImage imageNamed:#"Thumb"] forState:UIControlStateHighlighted]; // slider thumb on touch
[slider setMinimumTrackImage:[UIImage imageNamed:#"rMin"] forState:UIControlStateNormal]; // thumb left part
[slider setMaximumTrackImage:[UIImage imageNamed:#"Max"] forState:UIControlStateNormal]; // thumb right part
choose images carefully, now its photoshop work

Size of default thumb image for iOS slider?

What is the exact size (width, height) of the (default) thumb image for the iOS slider? Is there perhaps some clever way to coax this out of the system (XCode, iOS)?
I tried
int thumbWidth = slider.currentThumbImage.size.width;
which I found here on this site, but it comes back with 0.
Additional question: The Xcode debugger shows this undocumented variable in the UISlider: CGFloat _hitOffset. Does anyone by chance know what it is and what it's for?
What about this? Works for me:
CGRect trackRect = [self trackRectForBounds:self.bounds];
CGRect thumbRect = [self thumbRectForBounds:self.bounds trackRect:trackRect value:0];
CGSize thumbSize = thumbRect.size;
If you want change UISlider appearance then use below method
[[UISlider appearance] setThumbImage:[UIImage imageNamed:#"yoursliderimage.png"] forState:UIControlStateNormal];
as well as below code would change you slider track also
UIImage *white = [UIImage imageNamed:#"16x16white.png"];
[movieTimeControl setMinimumTrackImage:[white stretchableImageWithLeftCapWidth:3.0 topCapHeight:0.0] forState:UIControlStateNormal];
[movieTimeControl setMaximumTrackImage:[white stretchableImageWithLeftCapWidth:3.0 topCapHeight:0.0] forState:UIControlStateNormal];
Aplle HIG document doesn't define any size for UISlider thumb image but it should be under normal image size.
The best way is using a Resizable Image.
But the slider thumb size is 23x23 if you don't want to make the image resizable
There is a session video about UI customization in the WWDC 2012 collection:
https://developer.apple.com/videos/wwdc/2012/?id=216
You might find your answer here. Its about UISlider customization too
Here is the swift 4 version:
yourSlider.setThumbImage(UIImage(named: "Knob"), for: .normal)

momentum rotation of image in iOS

I'm new to iOS graphics and animation and would like to know to how to accomplish the effect of momentum rotation of an image upon flick as seen in the video below.
http://www.youtube.com/watch?v=ZIQs-OWgkgI (now broken)
Even when not flicked the images have a nice sway.
Thanks.
That is pretty slick. It is rotating the view around a point (center of top) and then changing the amount of time it takes to rotate algorithmically, reversing when appropriate. I can't give you code, but I think if you watch this video demo from Prof. Hegarty you will have the tools you need. He rotates around a point outside the view - you can simply rotate around the edge of the view (and don't shrink the view). Check it out:
Part 1: http://www.stanford.edu/class/cs193p/cgi-bin/drupal/node/291
Part 2: http://www.stanford.edu/class/cs193p/cgi-bin/drupal/node/293
You'll definitely want to download the videos off iTunes U (free), as there is a lot of explanation.
Good luck,
Damien
I checked with the author of this and was informed that it was implemented using the Box2D physics library. I'm going to give that a shot. Thanks for the responses!
//this code can be used to rotate an image having both back and front
rotate = [UIButton buttonWithType:UIButtonTypeCustom];
[rotate addTarget:self action:#selector(rotate1)forControlEvents:UIControlEventTouchDown];
rotate.frame = CGRectMake(137.5, 245, 45, 46);
[rotate setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:#"viewing.png"]] forState:UIControlStateNormal];
[self.view addSubview:rotate];
int count;
count=0;
-(void)rotate1
{
count=count+1;
NSLog(#"rotate");
[UIView transitionWithView:imagecircle // use the forView: argument
duration:1 // use the setAnimationDuration: argument
options:UIViewAnimationOptionTransitionFlipFromLeft
// check UIViewAnimationOptions for what options you can use
animations:^{ // put the animation block here
imagecircle.image = imagecircle.image;
}
completion:NULL];
if(count%2==0)
{
NSLog(#"image.str.%#",appDelegate.imageNameString);
[imagecircle setImage:[UIImage imageNamed:appDelegate.imageNameString]];
[labellocation removeFromSuperview];
[labeldate removeFromSuperview];
[self.imagecircle addSubview:labelfrom];
}
else
{
[imagecircle setImage:[UIImage imageNamed:#"TGP_BACK.png"]];
[labelfrom removeFromSuperview];
[self.imagecircle addSubview:labellocation];
[self.imagecircle addSubview:labeldate];
}
}

UIButton doesn't listen to content mode setting?

firstButton is a UIButton of type Custom. I'm programmatically putting three of them across each cell of a table, thusly:
[firstButton setImage:markImage forState:UIControlStateNormal];
[firstButton setContentMode:UIViewContentModeScaleAspectFit];
[cell.contentView addSubview:firstButton];
Elsewhere, I'm telling it to clipToBounds. What I get is a crop of the center square of the image, rather than an aspect-scaled rendering of it. I've tried this lots of ways, including setting the mode property on firstButton.imageView, which also doesn't seem to work.
I had the same problem. I see this question is a little old, but I want to provide a clear and correct answer to save other folks (like me) some time when it pops up in their search results.
It took me a bit of searching and experimenting, but I found the solution. Simply set the ContentMode of the "hidden" ImageView that is inside the UIButton.
[[firstButton imageView] setContentMode: UIViewContentModeScaleAspectFit];
[firstButton setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
Perhaps that's what Dan Ray was alluding to in his accepted answer, but I suspect not.
If you're dealing with the UIButton's image (as opposed to it's backgroundImage), setting the contentMode on the UIButton itself or on its imageView has no effect (despite what other answers say).
Alternatively do this instead:
self.button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
self.button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
Or size your image accordingly.
OR just use a UIImageView (which properly respects contentMode) with a UITapGestureRecognizer attached to it, or a transparent UIButton on top of it.
Rather than setting the contentMode on the button itself, you'll want to set contentHorizontalAlignment and contentVerticalAlignment properties and (crucially) the contentMode for the button's imageView for any kind of aspect fill or fit:
button.contentHorizontalAlignment = .fill
button.contentVerticalAlignment = .fill
button.imageView?.contentMode = .scaleAspectFit
You can also do other things like aligning the button's image to the top. If you don't need an aspect fill or fit, you just can set the alignment by itself:
button.contentVerticalAlignment = .top
After a couple of hours of confusion, here's how I got it to work under iOS 3.2. As dusker mentioned, using setBackgroundImage instead of setImage did the job for me.
CGRect myButtonFrame = CGRectMake(0, 0, 250, 250);
UIImage *myButtonImage = [UIImage imageNamed:#"buttonImage"];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setBackgroundImage:myButtonImage forState:UIControlStateNormal];
[myButton setFrame: myButtonFrame];
[myButton setContentMode: UIViewContentModeScaleAspectFit];
The answer is to use a UIImageView with all the lovely Content Mode settings you want, and then layer a custom button on top of it. Dumb that you can't do that all in one shot, but it appears that you can't.
These two things (which are quite hard to find initially) will stretch your UIButton image to fit the button size:
one should always try to set such in the Storyboard rather than code.
Found a fix for this. Set the adjustsImageWhenHighlighted property of UIButton to NO.
UIButton *b = [[UIButton alloc] initWithFrame:rect];
[b setImage:image forState:UIControlStateNormal];
[b.imageView setContentMode:UIViewContentModeScaleAspectFill];
[b setAdjustsImageWhenHighlighted:NO];
Hope this helps. Feel free to comment below, I will follow up on any questions that you have.
My answer is similar to Kuba's. I needed my image to be programatically set.
UIImage *image = [[UIImage alloc] initWithContentsOfFile:...];
[button setBackgroundImage:image forState:UIControlStateNormal];
button.imageView.contentMode = UIViewContentModeScaleAspectFill; //this is needed for some reason, won't work without it.
for(UIView *view in button.subviews) {
view.contentMode = UIViewContentModeScaleAspectFill;
}
Only solution which worked for me:
[button setImage:image forState:UIControlStateNormal];
button.imageView.contentMode = UIViewContentModeScaleAspectFill;
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;
Swift 3
self.firstButton.imageView?.contentMode = .scaleAspectFill
For anyone experiencing this on iOS 15 and Xcode 13, see Matt's answer in this other question.
The behavior of Xcode changed and now defaults UIButtons from the library to the plain style, which prevents the child image from scaling as expected.
Instead of setImage try setBackgroundImage
I believe we have a simple interface builder issue here - apparently the IB ignores any content-mode changes AFTER you have set the image-property.
the solution is as simple: set the content mode, remove previously set image-names (make sure you remove it in all states, default, highlighted etc.), then re-enter the desired image-names in all desired states - et voilĂ .
I also advice to have a look at the adjustsImageWhenHighlighted UIButton property to avoid weird deformations of the image, when the button is pressed.
In trying to figure this out, my method got a bit hackier as time went on, and I wound up subclassing UIButton and overriding setHighlighted:
For me it works to just knock down the image alpha to .5, because they're on a black background.
However, it only works if I comment out [super setHighlighted:] (where it appears the image-stretchy code is going on), which just doesn't feel like the right way to solve this at all...everything seems to be working fine, though. We'll see how it holds up as I keep working on it.
- (void)setHighlighted:(BOOL)highlight {
if (highlight) {
[self.imageView setAlpha:.5];
} else {
[self.imageView setAlpha:1];
}
// [super setHighlighted:highlight];
}
If anyone looking for answer that work in iOS 6 and iOS 7 and storyboard:
You can set image in your storyboard:
And then:
for(UIView* testId in self.subviews) {
if([testId isKindOfClass:[UIImageView class]])
[testId setContentMode:UIViewContentModeScaleAspectFill];
}
If the UIButton does not seem to listen to the layout constraint settings, do check whether the images are larger than the button size. Always use the #2x and #3x images for retina resolutions.

Resources