AspectFill does not fill the UIImageView - ios

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;

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).

Programmatically set UIBarButtonItem fill color?

I have a set of UIBarButtonItems from png files inside a UIToolbar.
When a user clicks on an icon, I want that icon to be filled with a color to indicate a state change.
Is it possible to do this with a single set of images (maybe by programmatically changing some attributes) or do I necessarily need two sets of images (on for each state) ?
make sure your UIImage's renderingMode is UIImageRenderingModeAlwaysTemplate first
if you initWithImage then change UIBarButtonItem's tintColor.
Although UIBarButtonItem is not a view, its tintColor property behaves
the same as that of UIView.
else if you initWithCustomView in which with a UIImageView then change the imageView's tintColor might work too
else you can setItems: animated:NO to UIToolBar every time after clicks which might not be a elegant one
It is very straight forward. You just need to do the following in code :
imgView.image = [imgView.image
imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
imgView.tintColor = [UIColor someColor];

Why does the Title in my UIButton misalign vertically in iOS8?

I have a UIButton created in a Storyboard. It has no special constraints / insets or anything. The only changes in code to it are :
[btn setBackgroundImage: [UIImage imageNamed:#"SomeImage"]];
[btn setTitle:#"Done"];
However, the button seems to be middled up whenever I use it in Autolayout :
The only way I could get it back was when I changed the background image to something else, like in :
So there seems to be something rather messed up with the Background image. However, I don't get why it doesn't work badly in pre-iOS8 then. Also, when I changed the insets :
self.closeButton.contentEdgeInsets = UIEdgeInsetsMake(-15.0, 0.0, 0.0, 0.0);
Then I got the content inside the Button again. But now, the iOS-7 button was misaligned. Can someone help me out over here?
This is an iOS bug, fixed in iOS 8.1

Add an image inside UIButton as an accesory

I have the following UIButton I want to add an Image inside to the far right. Like an accessoryView
I am already using backgroundImage and I want to avoid combining the 2 images into 1.
Is it possible to add another image inside the UIButton that is an AccesoryView lets say an image that has a PLUS to the far right? ( I am looking to a way to insert the new image inside the Button)
#luisCien comment
I tried
[_addImage setImage:[UIImage imageNamed:#"shareMe"] forState:UIControlStateNormal];
_addImage.contentHorizontalAlignment =
UIControlContentHorizontalAlignmentRight;
and it looks like this, I would like to image to be on the right side of the text.
Just add it as a subview and choose your coordinates..Try something like this:
UIImageView *yourPlusSign = [[UIImageView alloc]initWithImage:[UIImage imageNamed:#"yourPlusSignImageTitle"]];
yourPlusSign.frame = CGRectMake(x, y, width, height);//choose values that fit properly inside the frame of your baseButton
//or grab the width and height of yourBaseButton and change accordingly
yourPlusSign.contentMode=UIViewContentModeScaleAspectFill;//or whichever mode works best for you
[yourBaseButton addSubview:yourPlusSign];
You can use the contentHorizontalAlignment property of UIButton and set it to UIControlContentHorizontalAlignmentRight this will position the image you add to it to the far right.
Edit:
As suggested by #danypata you can also use UIButton's property imageEdgeInsets to leave
some margin around your 'accessoryView' like so:
[_addImage setImageEdgeInsets:UIEdgeInsetsMake(5, 0, 5, 10)];
Regarding having the text on the left and the image on the right, I believe it's not possible (someone please correct me). For that I would either create my own custom button by extending UIControl or add a UILabel and set it's frame (as opposed to using UIButton's setTitle:forState:) and add it as a subview of the button.
Hope this helps!

How to not stretch an image in UIButton

I'm trying to create a custom UITableViewCell programmatically and one of the subviews of this cell is going to be a button with an image in it (a simple image of a magnifying glass). However, I want the button's image to be centered and scaled proportionately down to fit in the button and NOT to be stretched to fill the entire button. Below is my code where self refers to the custom UITableViewCell which I am placing the button into.
self.myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[self.myButton setBackgroundImage:[UIImage imageNamed: #image_name_here"] forState:UIControlStateNormal];
self.myButton.frame = CGRectMake(...//something, something)
self.myButton.imageView.contentMode = UIViewContentModeCenter;
[self.contentView addSubview:self.mySearchHelpButton];
Right now the image stretches to fill the entire button rather than scaling proportionately so that it fits nicely.
I have also tried setting the contentMode to UIViewContentModeScaleAspectFill but this doesn't seem to change anything. In fact, none of the different contentModes seem to change anything.
Have you tried setImage instead of setBackgroundImage?
Make sure the button is a Custom UIButton
Just using setImage: did not work for me on iOS9.
But it worked combined with myButton.imageView.contentMode = UIViewContentMode.ScaleAspectFit;
In Swift...
button.imageView?.contentMode = . scaleAspectFit
Storyboard
Property image.
You must define specific property in Runtime Attributes of Identity inspector – imageView.contentMode, where you set value relatively to rawValue position of enum UIViewContentMode. 1 means scaleAspectFit.
And button's alignment, in Attributes inspector:
Swift 4.2
myButton.imageView!.contentMode = .scaleAspectFit
Swift earlier version
myButton.imageView!.contentMode = UIViewContentMode.scaleAspectFit
Swift 4.X and 5.X
button.imageView!.contentMode = .scaleAspectFit
button.contentVerticalAlignment = .fill
button.contentHorizontalAlignment = .fill
I think you guys are missing the obvious issue. The image is too large for the button and iOS then has to guess how you want to scale it. Make sure your images are the right size and you won't have this issue. Of course this is for static images and buttons that you know the size for up-front -- not dynamic content.

Resources