Xcode how to make flexible constraint in Autolayout - ios

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.

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.

Image view autolayout to pin 60pt from horizontal edges, but auto scale height?

I'm trying to figure out an approach to the following:
I have a square image, I want to align it horizontally and vertically in the centre of the screen and constrain it in a way where its left and right edges are 60pt from screen edges.
I got thus far, but am now getting an error from XCode about top and bottom image constraints. How can I make it so image keeps its square ratio and auto resizes based on screen size?
Example, images width became 294pt in screenshot below, yet height remained unchanged, where as it should change to 294 pt as well.
You need to add 3 more constraints.
Vertically align your view to superview
Horizontally align your view to superview
Set aspect ratio of your view to 1:1 (Important)
On the whole you should have 5 constraints. The screenshot is given below.
this error occurs when auto layout did not find enough information about a component on the storyboard. as you did it here you only provide the trailing and leading constraint so now it asks for height and width.
if you want the same height on every device selects the height constraints that will fix the heigh 300.
Or simply you can do this set the alignment constraint to Center vertically and horizontally and then add top and leading constraints

Auto layout issue for devices in the same family and orientation

I'm working on my beginner app and checked on the preview option for my app. I've noticed that across the iPhone X, 8+, 8 and SE the spacing of my objects are off. I've learned about the custom size class for various devices for different orientations but nothing what said about the same device family in the same orientations (ex: iPhones in portrait mode). This is what I am seeing.
The iPhone X looks fine but the others, not so much. I'm guessing that instead of using hard numbers for auto layout I should be using a ratio based on the height and width relative to the screen. Is that right?
Try to take Base ViewController as iPhone4S. Drag UIView called as HolderView, which should hold that UILabel and Two UIViews.
As per Design, give HolderView's width and height be same. Like Square shape.
Give Constraints as, left and right as 20px, CenterHorizontally and CenterVertically, and finally give AspectRatio to itself.
HolderView
Add UIView for UILabel to HolderView called as LabelHolderView.
Give constraints for LabelHolderView as left and right 20px, CenterVertically and Height as 90px (approx - Two lines for UILabel)
Click CenterVertically constraints and decrease Multiplier value to 0.4.
You can get answer from here that how to change multiplier value.
LabelHolderView
Add UIView for Two Action to HolderView called as ActionHolderView.
Give constraints for ActionHolderView as left and right 20px, CenterVertically and Height as 160px (approx)
Click CenterVertically constraints and increase Multiplier value to 1.3.
Inside that ActionHolderView, drag Two UIView called as PhotoView and NoteView and Embed in UIStackView as Horizontal axis, Fill Equally and 8 as Spacing.
Give constraints for UIStackView as top, left, right and bottom as 0.
ActionHolderView
Change all UIView's BGColor as per your design. Then, add UILabel and UIButton and UIImageView to respective UIViews.
For UILabel, top, right, left and bottom as 0. (number Of lines as 2)
For Two UIButtons ,right, left and bottom as 8 and height as 30
For UImageView, Width and Height be 50px (Change width and height from Size Inspector - Square shape). Give Constraints as CenterHorizontally, CenterVertically and AspectRatio to its SuperView and AspectRatio to itself
ViewController
You need to set height/width constraints for your items. I would also put your add photo/add note buttons in a view to make it easier for constraints. Don't be afraid to mess around with it, you can always to command z to restore the previous version.

Square UImageView in a Cell of UITableView

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.

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.

Resources