Image Height Constraint Not Working - ios

I have a UIImageView and used autolayout to add height and width constraints (and other ones as well). There are no conflicting constraints or warnings. I am querying an image from a database and setting the imageview's image to that image. However, the image is showing up with a much larger height than I have specified via constraints. How do I fix this?

This line should resolve your problem :
yourImageView.clipsToBounds = true
You can do it in Storyboard by checking the "Clip Subviews" of your UIImageView element.

1
YourImageView.contentMode=UIViewContentModeScaleAspectFit;
May be the problem is coming because of your contentMode ,so please your imageView content mode.
2.if you are developing application for a single phone then use aspect fit for that[constraints]

If you are getting image from server then you should resize the image according to your imageView size ...its a best practice.

Related

How to add image in ImageView of TableController properly?

I have a TableViewController and custom cell. Inside of the cell i have an ImageView. Xcode 9.4
*I do not have enough reputation - so provide links.
Click for image
I have constraints settled to superview Leading-Trailing: 20, Top-Bottom:10, clip to bound enabled, Scale to fill.
When i launch application, i oversee this:
Click for image
Image shrinks down. If i change to Aspect Fit/Aspect Fill the image becomes smaller(image view is not filled) or zoomed(not all image is available).
How to fix this issue? I do not want to give it specific width/heigh for compatibility.
Maybe i should change my constraints? I have watched several tutorials, where these constraints work, even did step by step, however it does not work as supposed to.
All constraints are settled for ImageView to superView (that is cell itself).
Thanks.
Set estimated height for your cell self.tableView.estimatedRowHeight = CGFloat(44) and also tableView.rowHeight = UITableViewAutomaticDimension so that it will grow according to your content inside. the problem here is with your image your cell getting height from your image you provided if you give fix height for your image your problem would be solved.
to adapt height for imageView according to iPhone and iPad.
First way is to get superView height and give your image height according to that programmatically.
Second you can get current device type and give your image you height to that UIDevice.current.userInterfaceIdiom == .pad. you can't do it in storyboard because you don't have access to superView in custom cell.

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 with contentMode AspectFit break Autolayout height in iOS7 but working properly in iOS8

According to my previous question.
Autolayout for Height working in iOS8 but not working in iOS7
I use UIImageView which will display the image from server once the download finished. I have autolayout constraint to define the height of UIImageView which I set via IB and has an outlet.
In my code, when I finished download the image I change the height of autolayout to match the height of my image. It works perfectly in iOS8 but in iOS7 which I tested via simulator always display the UIImageView height bigger than image size make the vertical area of top and bottom blank
Please see the screenshot here
I totally don't understand. I didn't get any warning but the autolayout just didn't work to keep the UIImageView height in place to what I set it via [layoutConstraint setConstant:]
Please help
In your other question you set the image view's content mode to "centre".
Try setting it to "Aspect Fill" or "Scale to Fill" according to what you need to achieve.

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.

iOS 7 Autolayout doesn't work

I've been working today on this problem for a while now and i couldn't solve it. I downloaded Apple's Autolayout Guide and couldn't find an answer/solution. The problem is that I'm adding a UIImageView to my View Controller and when I change the windows form factor from 3.5 to 4 and vise versa, the image view is stretched.
To solve this, I tried many ways such as clicking on reset to suggested constraints, clear all constraints, and build my own constraints by pinning and aligning.
By aligning and pinning I set the height and width to the size as in the Interface Builder and the horizontal and vertical positions as well. Unfortunately, the image still stretches upon changing the form factor in Interface Builder. I tried to only pin the height of the UIImageView but it doesn't work.
How can i work this out?
Autolayout only deals with the frame/bounds of the UIImageView. The actual way the image is rendered is controlled via it's contentMode property. You need to set it to either UIViewContentModeScaleAspectFit or UIViewContentModeScaleAspectFill to avoid stretching.

Resources