How to make sure the image is allways positioned to the top? - ios

I use UIImage to display my test image with Aspect Fit mode. I want it to be always positioned to the top and fill as much space as it can (see image below).
How to setup constraints, so that the image will be always positioned to the top (without any gap on taller devices)?
Additional info:
I'm using Xcode 7.0.1
Developing for iOS 8 and above.
I'm using auto layout.
I've tried to set " >= 0" to the bottom constraint, so that the bottom gap could get a double height, but it didn't work.
Any suggestions?

I suggest you programmatically calculate the size of the imageView and set the frame directly.
Do so in your viewWillAppear or viewDidLoad method of your viewController.
this will do the trick
UIImage *image; // yourImage
CGFloat whRatio = image.size.width/image.size.height;
if (whRatio > 1) {
// there will be borders on top -> adjust your view
imageView.frame = CGRectMake(imageView.frame.origin.x,
imageView.frame.origin.y,
imageView.frame.size.width,
imageView.frame.size.height / whRatio);
}
You might want to think about also adjusting the imageView in case the image is narrow, to prevent white spaces completely.

I think you have the correct constraints. Did you try to set the contentMode to "Scale to fill".
maybe you are using aspectFit

Here I'm adding an image showing how constraints are setup for the view that contains the image, as well for the image inside the view.
If anyone want to look at my experiments in details I've made a public repo via the link below. It includes the storyboard solution branch as well as code solution branche.
https://bitbucket.org/AndrejBB/test-layout

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.

Image Height Constraint Not Working

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.

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

UIImageView inside custom UITableViewCell - shows a little distorted image

I'm using UITableView with custom cells to create my own selection table.
Each cell has its own UIImageView, it's pinned to the right side of cell and there are three constraints on it: right trailing, top and bottom are pinned to these of the superview.
The image I use to show has template render style, there are 3 different sizes for different screen resolutions (16x16,32x32 and 48x48).
Everything works just fine, except one little quirk: the displayed image seems to be shifted up for one or two pixel lines, and these lines are added to the bottom of the image (at least it looks like that).
The view mode is set up as "Center", I tried others, but all modes that do not scale the image (I just want it to show as it is) have the same quirk.
I tried everything I could think of, but no success - the quirk's still here. In the Xcode view hierarchy everything shows just perfect! No quirk!
Has anyone seen anything similar? Are there any secret magic actions to avoid such an effect? My head explodes :(
Here are the screenshots: https://yadi.sk/d/rtTqVqcYfPJB3
In storyboard you want to set the UIImageView mode to either aspect fit or aspect fill.
Aspect Fit: fits your image proportionally inside the UIImageView so you might end up with white space
Aspect Fill: fills the UIImageView with you image proportionally but you might have some overflow that cuts off part of your image
You can also do this programmatically:
imageView.contentMode = UIViewContentModeScaleAspectFit;
imageView.contentMode = UIViewContentModeScaleAspectFill;
I have fixed the quirk by one (quite straightforward) step: by adding two blank lines, one line to the top, one line to the bottom of the icon.
So resulting icon sizes in my case: 16x18, 32x34, 48x50. The quirk is gone totally, but the reason for this weird behavior is still unknown for me.

Resources