AutoLayout - Not able to set UIImage Width as desired - ios

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.

Related

Xcode how to make flexible constraint in Autolayout

hello everyone I've get stuck in Auto-layout issues
I have login storyboard that contains three Items Image-view , Stack-view , Button like the first image below
I've tried to add constraints using two ways:
The first way I've added "pinned" constraint to Image-view as shown in Photo1 below but when I've changed to landscape the Image was disappeared as the second photo below.
please notice I haven't got any warnings
The second Way I've tried by wrap the Image-view using view then i added
"Pinned" constraint to view
finally i added fixed width & height to Image-view and Horizontal & vertical in container but I've got this result as third and fourth photo
what should i do to make my Image-view Adaptive for all Screens ?
thanks
Photo1
Photo2
Photo3
Photo4
As when changing to landscape screen height changing a lot. the problem with your design with Autolayout is it's not adaptive with landscape and portrait mode because you are give fixed hight and width to your layout.
to make your image-view Adaptive for all screens give your image-view height to superview height in percentage so when in landscape mode height will change your image-view will still gets the height from superview height in exact same percentage.
have a look at this demo hope this will help.
Autolayout
First drop UIImageView into ViewController.
Give it a top constraint and Align it horizontally to it's SuperView.
As now it's only required height and width.
Then Ctrl drag from ImageView to itself and select aspect ratio. give
a aspect ration 1:1 (it's just telling storyboard if any of height or
width is available i want the other to be the same like if width is
given then height should be the same as width, vice versa for height) as you may want
perfect square shape for your profile ImageView.
Now Ctrl drag from ImageView to SuperView and select Equal height.
As now your ImageView got the height to SuperView Height.
give your ImageView proportional height (percentage, Aspect
Ration or Multipiler) to SuperView how much you wanted to give like
so.
At the end your UIImageView will get the height proportional to SuperView height, vice versa for width.

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 aspect ratio imageview in tableviewcell

I have a custom tableViewCell with an imageView inside it. Layout as such:
I want to use autolayout to calculate height of the cell in method
CGSize size = [cell.contentView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize];
so I set up constraints like this
Basically these constraints just pin each element to the edge of its superView or to the view above/below.
The problem is that there is one more constraint I want to add which is the imageView's aspect ratio. I want set imageView's height is always half of its width. If I directly set up aspect ratio constraint to 2:1 I always get Unable to simultaneously satisfy constraints error. I tried change the priority to high and still no luck.
Can I achieve this by using autolayout?
The reason why your constraints work and the automatic height is correctly calculated is that all the UI elements have their own intrinsic size (the size that would make it fit their content).
Therefore, the imageView already has a size to fit the image you put inside. You can't both use the intrinsic size and specify your own (even if you just want to change one of the dimensions).
A solution I see would be to have a static height constraint on your UIImageView, plus the aspect ration one for the width. Then in the code you set the constant for the height constraint to the image.size.height value.
[self.imageHeightConstraint setConstant:self.imageView.image.size.height];
[self.view layoutIfNeeded];
In this way you sort of keep half the intrinsic size (just the height) and get custom width.
Hope my explanation made sense 😊. Let me know if you have questions though.

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

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.

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