UIButton background image horizontal scale to fill while maintaining aspect ratio - ios

I am trying to set the backgroundImage of a UIButton to an image that could be any size. The way I want to display tall images is for the images to expand to fill the button horizontally, and only show the top part of the image. So for a button with dimensions 100x200 and an image of 80x400, I want to scale the image to 100x500 and only show the top 200 pixels of that scaled image.
I am wondering if there is some way to alter how the background image fills the uibutton (right now it would not preserve the aspect ratio of the image). I tried setting the contentMode of the button and the button's imageView (note I set the button to a Custom Type, not System) to .ScaleAspectFill but that did not do anything.
The closest I came was by ditching the background image entirely and using setImage instead. This code was able to fill the button horizontally, but it shows the middle of the image, not the top. It also had odd functionality where if the scaled image was the same height as the button, it was not showing the full image (about 5 px were cut from the bottom and top). Here is that code:
imagesButton.imageView?.contentMode = .ScaleAspectFill
imagesButton.contentHorizontalAlignment = .Fill
imagesButton.setImage(image, forState: .Normal)
I have looked at about 5 stack overflow posts regarding image resizing in UIButtons, but I can't find the right set of settings that solve my particular need.

I don't know if this solution is an option for you, but the best way to achieve what you're trying to do, and give you pretty as much flexibility as you'd like with positioning and scaling the image, it to use a UIImageView (maybe even with a UIView that wraps it, for more image processing options) with a transparent custom button on top of it.
The upside to this approach is that you'll be able to do pretty much anything you want with the image and the way you display it. The downside is that you lose some of the default UIButton features (such as automatically altering the image when touched). But you can achieve these features programmatically if you want.

Related

Center UIButton with image next to it

I'm trying to create a UIButton where the facebook icon is a part of the centering, however i can't seem to figure out how to make it part of the centering process. So far i've just created a UIButton with the text. I could just create a image next to it, however then it will not be part of the centering.
You can easily set image and title both for your button for different control state like normal,selected or highlighted etc.
So, set your image and title to your button from storyboard or programmatically there is no need to take separate imageview for image.
You can set edge insets for title and image both from interface builder or programmatically. you can manage top or bottom edge insets for title and image of button to manage it's vertical appearance!
check screen shot below,
Update : (as asked in comment)
For example,
this is the setup for my back button with image!
Provide button title and image for normal state. Then just change the left inset for image as per image

Setting gradient background color to LaunchScreen which will display a logo image at center

I have a requirement to set a Gradient background to the launch screen with a UIImageView in the center which shows the app logo . I tried having a view controller but I realised code will never be invoked.
I need some way to get behaviour as the code below does
self.view.backgroundColor = UIColor(patternImage: UIImage(named: "background.png")!)
I went through lot of answers about the same topic, most of the questions on gradient colour don't seem to have any additional requirements like showing another image other than the background .
I can hardcode the color in IB but i don't get the gradient.
You cannot run code or use any custom classes in the Launch Screen. Basically the launch screen can use an interface builder file and accommodate size classes / auto layout, but you cannot create a gradient in code like you have.
You'll have to supply a stretchable image, which contains enough data for it to be viable in all resolutions.
You can use 2 layers of UIImageView:
The first is large and contain the background gradient image, and a second layer of centered image (with transparent backgrount) of the logo.

iOS button backgroundImage vs image

for a button I see 2 fields: background image and image. Is the only difference that a background image allows for text on top of it?
These images serve different purposes.
The backgroundImage is the graphical representation of the button. This image typically represents the different possible states of the button (normal, selected, highlighted) and is usually stretchable (horizontally and/or vertically) to be usable with varied button sizes.
The image is content that sits within the button and is the graphical analogy to the text property.
A common usage pattern would be to use the same backgroundImage for multiple buttons in your app for a consistent theme but to use different images depending on the contextual meaning of each button.
Also, backgroundImage is not used for layout calculations. For example, [myButton sizeToFit] will take into account the image but not the backgroundImage.
Yes, it's the only difference I know

Center UIButton image with UIBarButtonItem on UIToolbar

I'm trying (everything in XCode with Interface Builder) to create a UIButton with image adding it to UIToolbar, which makes it UIBarButtonItem (when click twice it shows the UIButton properties).
I have a image#2x.png image with size 64x88 (height is 88, because in Retina it's the toolbar height). I'm not interested in non-retina screens. I added this image to UIButton setting Mode in View properties to Center (or Aspect Fit, it shouldn't really matter here; tried both, aswell).
Set its size to 32x44 (44 height in builder). Then, dragged it to UIToolbar.
Now, I want to change the width of my button to make it larger, because I want to give user some more space to tap, also, I planned the toolbar background for that. And I want the image on this button to remain the same, but centered. But, when I try to resize the UIBarButtonItem, it actually scales the image, no matter what Mode is set.
That's what happens (I'm showing an actual dragging process):
However, if I change image real size to height 44, it works well. But 44 height looks ugly in Retina devices, so, what should I do?
I've seen this happen. For the sake of argument, create two images, image.png and image#2x.png, and when setting the image of the button, select image.png in Xcode.
Alternatively, you can create a UIView-based UIBarButtonItem and have a UIImageView and a UIButton. This way, you have a more precise control over the image sizing.

UIButton with background image highlighted state wrong

I'm trying to use the standard highlighting logic for a UIButton but set the background image for the Normal state. When do this programmatically everything works fine, but when my button is set in Interface Builder I get the following results:
Has anyone run in this where the standard highlighting is changing the way the background image looks? Just to be clear I want the color change, just not the squared off corners on the left.
Figured it out. Apparently if my button size is smaller than the background image it scales appropriately for Normal State, but the highlighted state causes this strangeness. Good to know since I'm attempting to use a single image and just scale it for all of my various buttons.
In this case my IB button was set as 77pt wide, but my image is 97pt wide. It finally looks correct with my IB button set at 90pt wide or larger.

Resources