Square UImageView in a Cell of UITableView - ios

I'm trying to place an ImageView in a Cell, I want the Image to be square and to work on most iOS phones, iPhones and iPads.
I'm using constraints to do that, I made an aspect ratio but it doesn't seem to work right.
Do you have any idea how to do that ?

As said by Vladimir in the answer Auto Layout: Square Image View with equal width / height
you need to set this constrain to your imageview
First of all try to switch mode from w:Compact/h:Any to
w:Any/h:Any. By adding constraints in this mode you will apply them for all devices and orientations.
Here is list of constraints that you need to apply:
Set low priority for Equal Width and Equal Height to superview.

Related

Rescale UIImageView proprtionally in Launch Screen Storyboard

I have a UIImageView that needs to be centered and have a width that's equal to the screen width and some height that grows proportionally with the width. So for instance, for the iPhoneXR I have:
And on the iPad 9.7 it would be:
So basically the image just scales proportionally with each screen size. I have already figured out how to center it using constraints, but I'm lost on how to get it to scale proportionally. I've played around with the auto resizing options and constraints to no avail. I struggle so badly with these storyboards, wish I could just code it! Is this even possible in a LaunchScreen storyboard?
You can't execute any code inside launch screen. However, you can make it with constraints.
Set center equal to superview, then set width equal to superview. Now, you can add aspectRatio which is like 1:1, so you imageView height would always be equal to its width or any other ratio you want.
Example:
By adding proper constraint you will get your expected result. Add aspectRatio constraint to imageView.
All constraints as per
You can not give constraints to a view w.r.t viewController in LaunchScreen.storyboard because our view hierarchy has not loaded till that point of time.
You can give an imageView constraints w.r.t itself. CTRL drag on the imageView and select height, width or aspect ratio.
Though the recommended way will be to use another viewController that will work as LaunchScreen with some delay in segue.

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

Auto Layout: Square Image View with equal width / height

The title might seem a bit complicated so I'll just show you what I want in some pictures.
So here is what I want:
[1] - http://i.stack.imgur.com/5Vckr.png
And here is how I tried to accomplish it in xCode 6:
[2] - http://i.stack.imgur.com/ags9s.png
[3] - http://i.stack.imgur.com/UhIuy.png
As you can see I tried to use size classes to snap the ImageView to the left and right in portrait view and to the bottom and top in landscape.
But somehow it is not aligned right in Landscape and if I look at a iPad preview the ImageView does not show up at all.
This probably is pretty simple but I tried for hours and still have no result.
Thank you for your help!
First of all switch mode from w:Compact/h:Any to w:Any/h:Any.
By adding constraints in this mode you will apply them for all devices and orientations.
Here is list of constraints that you need to apply:
Set lower priority (e.g. 750) for the two constraints with the dashed border (Equal Width and Equal Height to superview), and let the others have default priority of 1000.

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