Button image stretching - ios

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.

Related

Resize an image that is in a button

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.

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

UIImageView Aspect Fill not filling view

I selected Aspect Fill under View > Mode for my UIImageView in my XIB but it stubbornly shows the same sized image.
I I
I I
I MY IMAGE HERE I
I I
I I
i.e. there is blank space above and below MY IMAGE.
What could I be doing wrong?
==== UPDATE
With a red backgroundColor:
RRRRRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRRRR
I MY IMAGE HERE I
RRRRRRRRRRRRRRRRRRRRR
RRRRRRRRRRRRRRRRRRRRR
Shouldn't Aspect Fill make it more like the entire rectangle is filled by something like MAG where MY I and E HERE are off-screen.
Doh. Turned out to be something stupid but I'm posting it here in case it saves someone hours of debugging.
Turned out I had AspectFill in the XIB and then AspectFit in my code (which trumped the XIB).
Have you specified the UIImageView to fill the screen?
If you specfify the mode you change the way the actual image will be displayed inside the imageView. But if your UIImageView does not fill the entire screen the embedded image will too not fill the screen neither.
You should specify the layout constraints to align the imageView with the bounds of its superView.
A easy way to test this is setting the backgroundColor of the UIImageView to something different than transparent - that way you can see where the image and where the imageView are
Another way to help debug this is to add a border. My issue was the image that design provided actually had a clear border around it. Chances are you're sizing it right and there's something wrong with the image itself.
I was experiencing a very similar problem only with a horizontally scrolling UIScrollView.
I had constraints on the scrollview and the UIImageView(s) loaded into the scrollview did not aspect fill properly.
Upon removing the constraints on the scrollview, .scaleAspectFill worked as it should.

UIButton background image not sizing right with autolayout?

I'm simply trying to fill the entire width of an iPhone screen with a login button for my users that is basically an image set as the background of a UIButton. This is the result I end up with (Notice it fills width as intended, but shrunken down in height):
My thought process was to make 3 images to compensate for #1x #2x #3x resolutions with different image sizes?
#1x: 144x33px
#2x: 288x65
#3x: 432x97
Now from there I thought I could just fill the width of my ViewController with the UIButton in Storyboard and set the background image of the UIButton which I would think would use the proper one automatically depending on device considering I put them in the right spots in Images.xcassets. I also set the trailing and leading constraints as well as bottom constraint to keep it filling left to right and stick to bottom. I just don't get how I would fix the height issue if it's looking fine in the actual XCode storyboard, but not in the preview or simulator as seen in above photo.
Any help would be tremendously appreciated.
The different image sizes are not used to replace each other when you scale an image. They are used to look nice when there are more actual pixels than software pixels.
For your purposes, you probably want to scale the image with the stretching rules or cap insets.
Check out this answer for an example:
https://stackoverflow.com/a/13367861/1864591

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