UIImageview in custom cell with Aspect fill mode constraints issue - ios

I have an issue with UIImageView in UITabelViewCell.
here is my UIImageView in the interface builder and it's constraints
When i set the mode "scale to fill" i got this result (which match the constraints and my expectations)
but I don't want that mode!!
What I want is "Aspect Fill", but sitting this mode give me this result
How Can I handle this issue ?

Using aspect fill keeps the proportions but may result in the image being drawn outside the bounds of the view. You need to clip the subviews on the image view (you can see this option in the interface builder screenshots above) and then the excess parts of the image will not be drawn.
If you want to keep proportions and also see the entire image, then you need Aspect Fit, but this will result in blank areas to the sides of the image if it doesn't match the aspect ratio of the view.

There is a constrains mismatch for your UIImageView,
Use below constrains only,
Top Space -> ContentView
Bottom Space -> ContentView
Trailing Space -> ContentView
Leading Space -> Only One Label
or
Top Space -> ContentView
Height -> 161
Leading Space -> Only One Label
Width -> 151

While selecting the content mode to Aspect Fill for the image view, the content is getting out of the bounds of the image view. This is because in aspect fill mode the content is filled till the time the bounds of the container (image view) are fully filled with the content by keeping the aspect ratio intact. And when the content is fully filled it get's out of the bounds either width wise or height wise.
In order to keep the content mode to Aspect Fill for UIImageView you should also set it's Clip Subview's property as well. Setting this property will clip the content of the image view outside the bounds.

Related

iOS Stackview inside ScrollView with preserve image aspect ratio

I'm having a problem with scrollable content. Basically, I want to place one ImageView and one Label on the screen with ImageView width equals screen width. height depends on intrinsic content aspect ratio (while width is always equal screen width).
The label is 20 spacing bellow ImageView. That means whole content height may less or equal or even longer than the root view. What I have tried is to put Label and ImageView inside a StackView (ImageView has Aspectfit setting), Then put StackView inside ScrollView. Set up constraints as below.
The problem is the distance between ImageView and top Superview and between ImageView and Label is too big (ideally should be 20). Is there any way to achieve the desired outcome?
PS: I have tried to set stackView distributtion but no help
tl;dr Remove the aspect ratio constraint and add a height constraint for the UIImageView.
It’s important to understand that UIImageView is basically a container that holds an image, and its dimensions don’t necessarily reflect the image’s dimensions (unless you manually make them equal). When you selected Aspect Fit as UIImageView’s Content Mode, the size of the container didn’t change to correspond the image’s size; this setting only changed the way the image is placed inside the container.
As I can see in your screenshot, the UIImageView’s height is greater than its width; furthermore, the aspect ratio is fixed by the constraint you’ve added. When you placed a landscape-oriented image inside this container, the latter left white bars on the top and the bottom of the image (just like the black bars you see when you watch a widescreen video on an old monitor). Change Content Mode to Aspect Fill or Scale to Fill to see the actual size of the UIImageView.
To fix this, remove the aspect ratio constraint and set up a fixed height for either the UIImageView or the UIStackView. If I were you, I’d probably set the UIStackView’s height equal to Safe Area so that no matter how tall the stack view is, it doesn’t go beyond the screen boundaries.

Resizing subview containing image in UIStackView

A while ago I thought it might be a good idea to replace my own layout for a number of views inside a scrollview with a UIStackView.
But after more than a week of frustration I'm asking for help:
I'd like to place subviews like these:
inside a vertical UIStackView (alignment fill, distribution fill). The UIImageView's content mode is aspect fill, the image is set at runtime and can change at runtime (especially its aspect).
I'd like the subviews to horizontally fill the stack view and to resize vertically in order to keep the image's aspect ratio intact.
When I don't add any special constraints, I don't get layout warnings, but the subview inside my stack view doesn't resize when I change the image.
If I add an aspect constraint to the UIImageView every time the image is being set, I get a layout warning the first time I change the image and afterwards it doesn't resize the subview.
Can someone of you auto layout wizards please help me out?
You cannot set a constraint on an ImageView based on an image that will be loaded later. What you should do instead is
Set a height constraint on your ImageView
Connect an IBOutlet to this constraint
When the image is loaded, find out it's aspect ratio
Calculate the desired height since you already know the width
(stackView's width)
Set this height to your ImageView's height constraint

How to maintain the aspect ratio of the UIImage placed at the top to the view?

In storyboard i am placing an UIImageView for the purpose of placing the profile picture. So this image is entirely at the top of the UIView.For this i have set the constraints like this:
Preview in iPhone7:
Preview in iPhone7plus:
The problem is that though the UIImage is correctly placed at the top centre but it is not growing in size as the screen size increases. So i think i need to reset the UIImage constraints for that . How to achieve the same?
The problem is you have set explicit constraints for height and width to each be 100.
What I would suggest is making a constraint which sets the width of your image view to be equal to the width of the main view. Then set the multiplier on that constraint to be something like .25 (or whatever works best for you). This will make the width of your image view always be 25% of the width of the view, so it will be larger on larger devices. Like so:
After setting that constraint, you should add an aspect ratio constraint for the image view (drag across the image view and let go, the select "Aspect Ratio"). Set that multiplier to be 1:1 and your image view will be a square.

AutoLayout - Keep Ratio of Image With Horizontal Constraints (Swift Xcode 6)

I've been trying to implement a timeline of picture (as instagram or facebook).
However, I've been struggling with the ratio of the image.
Here's my xib that contains my Cell :
I want the picture to keep its ratio and to completely fill the width of the cell.
I've set 4 autolayout constraint on the UIImageView as 0-0 for horizontal constraint and 5-5 for top/bottom constraints. I've also set the ViewMode to "Aspect Fit".
I've tried different setup :
set the height of UIImageView to a specific value :
--> The width of the image is modify to keep the image ratio
don't set the height of UIImageView
--> it considers that the height is equal to 0 and therefore doesn't even display the image
set the height greater or equal to a specific value :
-- > same behavior as 1. (The width of the image is modify to keep the image ratio)
I'm loading the image from an URL so I've been using the lib Haneke (https://github.com/Haneke/HanekeSwift) with the following function :
imageView.hnk_setImageFromURL(url).
It has an automatic resize of the image and I don't know how to get the original width/height of the image to set the height manually.
Again, what I want is the picture to keep its ratio and to completely fill the width of the cell.
Anyone has a suggestion ?
Thanks a lot !
This is the setup I came up with. It's a quick and dirty demo, but I would assume you'd want the image to always be centered. You can set the Horizontal and Vertical Center Constraint in storyboard to always keep it centered
If you want it to always completely fill the view, you can replace Equal Height with Equal Width, but by doing so, the imageView will run off the screen on some sizes because of the aspect ratio constraint
I've also included screenshots of the view at different sizes

How to scale a UIImage view to a square with Autolayout inside a UIScrollView

I've been struggling to learn autolayout (finally). I want to have a vertically scrolling UIScrollView that holds all the content for the view. I want to pin a UIImage view to the top of the scrollview and keep the image a square (scaleToFill). Basically I want to do this (only I want to do it with autolayout):
I can't get the image to keep its aspect ratio while staying within the screen bounds. Whenever I add an aspect ratio constraint the imageView grows to like 600 points (the width of the png I think), but I want it to just be as wide as the screen.
I think I am setting the constraints up for the UIImageView correctly because if I get rid of the scrollView and just drop the imageViw directly on the view then it seems to do what I want it to. The trick is putting it inside the scrollView.
This is how I currently have it set up:
The 4 vertical/horizontal space constraints on the scroll view are all set to constant 0.
Since the size of the UIScrollView depends on its content, you cannot have the size of your UIImageView subview dependent on it.
You have to remove the aspect ratio constraint of the UIImageView and use a fixed width one. You can also have your UIImageView width dependent on another view in the UIScrollView which has either a fixed width or otherwise unambiguous width.
Don't forget to use Placeholder as the intrinsic size value in Interface Builder.
Hope this helps.

Resources