Resize an image that is in a button - ios

I have a button with an image inside, but I want to adjust the size of this image, how can I adjust the size of the image?
I'm trying to make my image 150 points high, but I do not know how to do it programmatically

The best possible approach would be to regenerate the image asset at the appropriate size you need it (using photoshop, preview, etc) and then use that instead.
Another option, in the storyboard switch to the ruler tab and adjust the image insets.
Yet another approach would be to use a UIImageView and add a UITapGestureRecognizer to it to detect a tap. Then you can configure the image view with constraints and set up the aspect ratio. The downside here is that you lose the stateful changes of the button when it is pressed/depressed so it won't look as good. I wouldn't add an imageview as a subview of a button either, that can create strange problems.
One thing I'd like to add is that resizing images can be expensive. If you are loading these buttons into a tableview then you need to be careful not to accidentally resize the images as the cells are enqueued/dequeued. This will block the main thread and cause smooth scrolling issues.

When you set a button’s image it will have an imageView property containing the image. Therefore, you can set the imageView’s contentMode property to .scaleAspectFit to keep the image proportions after resizing:
button.imageView?.contentMode = .scaleAspectFit
The actual resizing can be done in InterfaceBuilder using layout constraints, for example.
However, if you know the button target size at compile time, you should make sure the image already has the desired size instead of sizing a wrongly-sized image at runtime.

Related

How to make an image in LaunchScreen.xib fill the background using Xcode

So I'm trying to setup a full screen background with a logo image centered in its middle with an existing LaunchScreen.xib (for a react native project) and I am having trouble with the autoformat.
I see that I can add constraints to my ImageView to maintain aspect ratio but I'm not sure how to make it fill(crop to fit) the view. When I test on device it just stays the same square as the interface builder and doesn't fill the screen. I've been trying to add different constraints to the image but it doesn't seem to affect it.
Also how do I preview it on different devices as shown in the UI. Selecting different devices doesn't seem to change the square canvas view at all.
BTW I don't want to do it as separate assets for every screen size - I'm curious to learn how to build it using the standard xib interface builder using layout rules.
The xib file was shown by a view that inside a UIViewController.
As you set constraints for UIImage in xib file (e.g. in UIView) you should set constraints of UIView inside the UIViewController.
Then now set constraints of root view to the edges, and set Content Mode of imageView to what you want as apple document said:
The scaleAspectFit and scaleAspectFill modes scale the image to fit or fill the space while maintaining the image’s original aspect ratio. The scaleToFill value scales the image without regard to the original aspect ratio, which can cause the image to appear distorted. Other content modes place the image at the appropriate location in the image view’s bounds without scaling it.

Zoom image returns to 1:1 if UIButton image is changed

I have a UIButton for brush, and another for zoom.
If I keep the images the same, both work well.
The images are attached to the buttons in Interface Builder:
If I change the button image for "draw", after a zoom action, the image will return to no zoom.
drawButton.setImage(UIImage.init(named: "Brush.png"), for: UIControlState.normal)
How can I change the image without affecting the zoom -- I want to be able to draw in a zoomed mode.
I have tried something like this:
drawButton.imageView?.image = UIImage.init(named: "BrushYellow.png")
This does not affect the zoom, but does not change the image. Ideas welcome please.
I tend to use the background image and force an aspect ratio equal to the aspect of the image on my buttons. It prevents all the pesky issues that come like what you are referring to.
Look at the actual aspect of the photo and set the constraint on the storyboard with an aspect ratio of the images dimensions. If you are creating it programmatically, then setting the frame with the ratio should work as well. Make sure you are using
UIButton().setBackgroundImage(image: UIImage?, for: UIControlState)
for changing the image or you will have overlapping images. If you need to set it for multiple control states, then do so, but I rarely find this necessary for anything other than .Normal
If you need to change the constraint then you can create an outlet to it and change its multiplier from 1:1 to whatever it should be and back. If the UI doesn't update itself you may need to call self.view.layoutIfNeeded()

Button image stretching

I am having a problem with the button image. I have a green background and an icon. The problem is that on larger screens the image stretches due to the button also stretch to the size of the screen.
I found several other topics they were talking about something, but none of them helped me solve this problem.
The next best picture explains the problem:
If you want your button to look like the 3rd image, you will have to use an image of that size and resolution.
Alternatively, you can set contentMode on your UIButton's imageView object. Use this line of code in your viewDidLoad function
button.imageView!.contentMode = UIViewContentMode.ScaleAspectFit
When your constraints increase the size of your button, the image used inside it will abide by the contentMode property set. Using those you can stretch it, which it's doing right now, or you can keep the aspect ratio, in which case it will show empty space on sides.
Play around with the content mode settings, try 'Aspect fit' instead of the currently selected.
It look like your constraints doesn't take on consideration the size of the asset.
You should set a Width and a Heigth constraints to your image asset.

UIImageVIew crop Image center according to ImageView size

I'm trying to create a UIImageView that will have a fixed size (i'm not using auto layout) and content image inside it will be centred.
The problem is that the image is big, and it enlarge my UIImageView - which corrupt the view i'm showing, so my only option is to Scale To Fit which make my image look bad.
Is it possible to do that with AutoLayout ?
AHHH - just use "center" mode on that selector.
And set CLIP SUBVIEWS checkbox.
Let us know if that fixes it.
For 2015 onwards, it's worth noting that there is no reason at all not to use autolayout.

issues in resizing UIImage on IB

I am facing a strange issue while resizing images in IB (Xcode 5). First, I drag & drop a UIImageView on my viewController in a storyBoard. I then resize and set an image for it, which works properly. However, once I try to resize the UIImageView control after setting the image, it starts behaving strangely. Sometimes it occupies the complete viewController, other times the image becomes so big that it is out of bounds.
I tried to set a smaller image, but it resulted in more strange behavior. Now if I try to resize it, sometimes it can't be resized by dragging. Sometimes the UIImageView is resizes properly but the contained image remains the same size. Any suggestions?
This was not happening when using Xcode 4.
There are two issues here.
For an image to be resized to the same size as its image view, it is essential that its content mode be set to UIViewContentModeScaleToFill, UIViewContentModeScaleAspectFit, or UIViewContentModeScaleAspectFill. You can make this setting in the attributes inspector.
NOTE: If you do not do this, then the image will be shown at its own natural size. If the image is huge, it will be shown huge. Moreover, it will be shown huge even if the image view is small, because the image view does not clip its image. If you want to the image view to clip its image (so that you can see where the bounds of the image view really are), set the image view's Clips To Bounds (clipsToBounds). Again, you can make this setting in the attributes inspector.
In Xcode 5, you are using auto layout by default. This means that the image view has the size of its image by default. If you don't want that, you must use constraints to set the height and width of the image view. You will then probably also have to use constraints to set its position. Do NOT run the app until all auto layout warnings in Interface Builder have been taken care of, or you will get a big surprise at runtime.

Resources