scale image in UIButton accordingly - ios

I have a UIButton, and I've added an image using the method setImage:
The image is smaller than the bounds of the button, and so it is centered as expected.
Now, if I resize the button I want the image to shrink too. (Currently if I resize the button, my image stays the same size since my resized button is still larger in bounds than the image).
Any idea on how to achieve this?

Just set the ContentMode of the ImageView that is inside the UIButton.
[[self.button imageView] setContentMode: UIViewContentModeScaleAspectFit];
[self.button setImage:[UIImage imageNamed:stretchImage] forState:UIControlStateNormal];
Hope this helps.

Related

Custom keyboard buttons iOS

I am trying to setup a highlighted state for keyboard buttons, but have some difficulties with it:
I can't set backgroundImage to be beneath the spaceButton Image, so it won't overlap shift_1 like on the image below (button is tapped).
I can't set up layer margins, so that there would be a space from the bottom and other buttons, and at the same time that spacing would function as that button.
Below is my code:
[self.spaceButton setImage:[UIImage imageNamed:#"shift_1"] forState:UIControlStateNormal];
[self.spaceButton setBackgroundImage:[KeyboardViewController imageFromColor:[UIColor colorWithWhite:0.4 alpha:0.5]]
forState:UIControlStateHighlighted];
self.spaceButton.layer.cornerRadius = 7.0;
self.spaceButton.layer.masksToBounds = YES;
I would be very thankful if anyone could help me.
You would have two ways:
By Image : Just add transparent pixel at bottom,left,right in image and create new image then set that image as in background image of button.
By imageEdgeInsets : Set button imageEdgeInsets and use to image property for set image(Highlighted/Normal state).

Change UISlider background with code

I have created a UISlider , and i want to change its look .
I dont want to change its image with my own, like this one
Change iPhone UISlider bar image
but, to create an elliptical UIView of my own,in code, and set it instead of the current slider thin line, than instead of the moving circle button, create a circle UIView in code, and replace it also.
I know i can set their images ,
[self setThumbImage:[UIImage imageNamed:#"switchThumb.png"] forState:UIControlStateNormal];
[self setMinimumTrackImage:[UIImage imageNamed:#"switchBlueBg.png"] forState:UIControlStateNormal];
[self setMaximumTrackImage:[UIImage imageNamed:#"switchOffPlain.png"] forState:UIControlStateNormal];
but how do i set my own costume uiview ?
If you want to make a circular Slider,you can check it once: http://code4app.net/ios/EFCircularSlider/52a6bdd9cb7e84af088b5fa8

AspectFill does not fill the UIImageView

I have a button that has contentMode set to UIViewContentModeScaleAspectFill. I set it like this:
self.itemImage.imageView.contentMode = UIViewContentModeScaleAspectFill;
I also set it to Mode - Aspect Fill in IB for the button, just in case. And when I set the image, I DON'T set is as a background image:
[self.itemImage setImage:image forState:UIControlStateNormal];
However, occasionally my image does not fill the button (which I want it to do), like in this case:
See grey spaces on each side. Occurs on both iOS 6 and 7.
Any ideas?
At long last, I managed to find the solution that works in Alfie's answer to this SO question.
In short, if your UIButton (or its hidden imageView) does not respond to contentMode, use this code:
self.button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
self.button.contentVerticalAlignment = UIControlContentVerticalAlignmentFill;

Why does a UICollectionViewCell with a UIButton have a monochrome/tinted image?

I create a UICollectionView and add a single cell who's only subview is a UIButton. That button has its title and image set. I've verified that the image data is correct in the debugger.
When the button is drawn on screen I see the text and the image however the image looks as if it has been filled with the tint color, obscuring all of the image other than its shape.
What am I missing here to have this show up as a normal button should?
Update
It turns out this is not specific to UICollectionView but rather all UIButtons in iOS7.
iOS 7 makes all images in buttons behave as template images using the alpha channel of the image in concert with the tint color to produce the image (much like the images in a tab bar). There's a new renderingMode property on UIImage which is defaulted to "automatic" which lets the context decide (which is template style for buttons)
This can be circumvented using the new imageWithRenderingMode: method on UIImage:
UIImage* myImage = [UIImage imageNamed:#"Foo.png"];
myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[button setImage:myImage forState:UIControlStateNormal];
The easiest way to avoid this is to use a different UIButtonType. It's UIButtonTypeSystem on IOS 7 that has this behaviour, so you could use a custom button instead:
UIButton *button = [UIButton buttonWithType: UIButtonTypeCustom];
[button setImage:myImage forState:UIControlStateNormal];
When the UIButton's background color lightText in this way, it will not close the button image.
UIButton.backgroundColor = UIColor.lightText

How do I place a button in a UIImage for determining different selections?

I have a UIView with a UIScrollView. I then added a gesture recognizer programmatically to the scrollView for left or right swipe.
I created 3 UIImageViews with a different UIImage each, say 1, 2 & 3.
Ive gotten it so each swipe moves the new UIImageView into the center of the view. But now I need to distinguish between the image tapped. The actual tap will kick off a server request and get the data but I must determine the UIImageView tapped because each UIImage is different and will present the user with the server response plus the tapped image.
Each UIImageView actually has a tag set; 1, 2 & 3. My question is, how do I add a UIButton/IBAction to it? I was thinking of adding a UIButton as "child" of each UIImageView but Im not sure it can be done and I dont know if its the best way to achieve what I want, which is to be able to distinguish which UIImageView was selected by the user.
I ended up using a button with a background image instead of the image with a button-like reactivity.
fourthButton = [UIButton buttonWithType:UIButtonTypeCustom];
[fourthButton addTarget:self action:#selector(buttonHandler) forControlEvents:UIControlEventTouchDownRepeat];
[fourthButton setTitle:#"SomeCardButton" forState:UIControlStateNormal];
[fourthButton setImage:[UIImage imageNamed:#"MyCard-Value.png"] forState:UIControlStateNormal];
[fourthButton setFrame:CGRectMake(0.0, 0.0, 40.0, 40.0)];

Resources