iOS Stackview inside ScrollView with preserve image aspect ratio - ios

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.

Related

How maintain a circular uiview using auto layout without fixed height and fixed width

I am having issues maintaining a circular uiimageview when using autolayout without fixed height and width.
Example with fixed height / width inside a uiview container that has a fixed height.
storyboard constraints
Circular image
in viewwillayoutsubview
profileImageView.layoutIfNeeded()
profileImageView.layer.cornerRadius = profileImageView.frame.size.height / 2
profileImageView.layer.masksToBounds = true
Example without fixed height / width inside a uiview container that has a fixed height.
Storyboard constraints
Circular image
Goal: What I am trying to achieve is create a circular uiimageview that scales for all device screen sizes. I believe the issue is that without fixed height and width my cornerRadius is not correct for a circle.
What would be the best way to achieve this without setting a fixed width and height as that does not work on all screen sizes.
The first problem is that you’re pinning your view to all the edges of its parent with a fixed margin.
That may well produce a square in your storyboard, but doesn’t necessarily on your device (it depends on if your parent is constrained as a square or not, which I can’t see in your screenshots). If the parent stretches it’s width to fit it’s own superview, but doesn’t adjust its height, then your image is stretched into a rectangle.
That looks like it is the case, as the image appears stretched horizontally.
I’d suggest you remove all constraints from your image view and start again.
Make the image have a width equal to, say, 0.8 of its superview, give it a 1:1 ratio, and center it horizontally and vertically with its parent.
The second problem, is that you may be setting the cornerRadius too early.
When the view is loaded from your Storyboard, its width and height are whatever it was in storyboard.
Once the view has been laid out in its parent, the width/height will be adjusted and you’ll be left with an incorrect cornerRadius.
To solve this, simply make sure you have a 1:1 aspect ratio constraint on the view, and set the cornerRadius in viewDidLayoutSubviews (instead of viewWillLayoutSubviews).

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.

UIImageview in custom cell with Aspect fill mode constraints issue

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.

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