Set UIImage height based on a ratio of the width using Autolayout - ios

I am trying to figure out how to set the height of a UIimage on the storyboard based on the width? I need it to be a ratio based on the width. So the image is full width.
Currently I am using the universal storyboard and want the image to be 3:4 so the height needs to be 33% greater than the width.
What I don't understand is the width is 600 on the universal storyboard so how do I then get the ratio of the height being 33% greater than the width?
I can't figure out how to do this.

If you add aUIImageView to the Storyboard and manually set it to the ratio you want, e.g. 300x400px, you can then click the pin button on the bottom right (shown below) and check the box that says "aspect ratio". That will add a constraint to always keep your view the same aspect ratio, even as you change the width.

Related

AutoLayout - Not able to set UIImage Width as desired

Following are the autolayout constraints I have added:
And the final result is as follows:
UIImage Content mode : Aspect Fit.
Label content hugging priority vertical : 253 (rest all are default for both imageview and label)
I have added a placeholder image until the image is actually downloaded. I have used 2 prototype cells in tableView - One with label. Another with Image and Label.
What I want to achieve is set the width of UIImage to 50% of screen size and let the height be calculated automatically based on aspect ratio.
cell.layoutSubviews also didnt help
Any help is really appreciated. Would like to achive this via storyboard itself if thats possible.
Thanks in advance
Set the width to 50% of the screen width. Programmtically it is written as imageView.frame.width = UIScreen.main.bounds.width / 2 I'm not sure how that would be done in storyboard. And set your desired aspect ratio constraint in the storyboard. The only constraints for the image should be its position in the cell, the aspect ratio, and the width. No height constraints.

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.

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.

How to use auto-layout to put things in a percentage of a screen size IOS

So I am trying to make my launch screen for an app. The launch screen is made up of 2 text boxes and 2 UIImageViews.
This is my sketch up of what it will look like
My problem is I can not get auto layout to work with percentages of the screen, instead it has these numbers that I dont quite know what to do with. So while I know that the two image views are a 4th of the screen width from the edges I dont know how to put it their. Also I dont know how to make the image size occupy the same ratio of space on all devices.
Could you please get me started on setting this up?
autolayout does not Have Percentage support but it supports Aspect Ratio.
you need to add one view and add your two imageViews and textFields add equal width and height constraint for ImageViews and same for TextField
you can also add constraints to ImageView to expand in rest of space for that just add margin constraints and equal width and Height of ImageViews.
add aspect Ratio to ImageView's Width and SuperViews width as your requirement.
you can add aspect Ratio with individually to width and Height.
here as you mentioned in que. you can add aspect ration 4:1 for 4th of the screen size.
do same for TextField.
if you still need help than tell me I can explain in depth with screenshot.

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

Resources