Auto-layout a basic thing [duplicate] - ios

How do I use auto-layout to make a child view fill its parent, or at least anchor all its edges to the parent view's edges?
I am using all four vertical and horizontal constraints in "Spacing to nearest neighbour", but when I run the app on a 3.5" device, the height of the view has not been automatically adjusted from 568 to 480 as expected.

Pin the top space to superview, bottom space to superview, leading space to superview and trailing space to superview.
Then set edit the constraints like so:
Superview.Top >= View.Top and Superview.Bottom >= View.Bottom

I would just set all "Spacing to nearest neighbor" to 0, and align horizontal and vertical center. That should do the trick.

Related

How to achieve this layout with auto layout ?

I m trying to create attached layout, I have added a superview and four subviews each view is aligned to one of the sides, to achieve the + sign in the middle .
Constraints I have added are: aspect ration to each subview and leading trailing margins, along with this also have added a width constraints to left bottom view.
But obviously this isn't working on smaller screen and everything gets messed up, not sure what exact constraints should i add so that I get the desired result on variable screen sizes.
create a superview with required aspect ration.
Add four subviews and make all equal width and equal height.
Pin each superview like "top, left" "top, right" "bottom, left" ""bottom, right""
Give one sub view with ratio according to superview width like if u want 10 px padding horizontally then give 320:155
And Height of one view according to their width.
Find the sample code made for this.
https://github.com/iTamilan/AutoLayoutPlus
You should use proportional height and width for the views, for eg:
For the Top Left view align Top Left view's top to top of the superview and similarly do this for leading. And now make width proportional to half the width of the super view minus half of the separation between the two views.
In short: Top left view's leading = superview's leading
Top left view's top = superview's top
Top left view's width = superview's width/2 - separation/2
Top left view's height = superview's height/2 - separation/2
Similarly do this for the rest of the views and you will be good to go, and your views will be fine on every device

Center image in LaunchScreen storyboard

I have an imageView in my LaunchScreen.storyboard. I centered it with two constraints:
Center Horizontally (Align Center Y)
Center Vertically (Align Center X)
I also have:
Width constraint equal to 84
What happens is that the image is just below the center. It's not perfectly centered.
Any suggestions?
Thanks!!
Give constraint as below:
1) Top space to superview
2) Bottom space to superview
3) Leading to superview
4) Trailing to superview
This will fill the whole nib with an imageview
One easy way to do it is to add constraints on each side that are equal to the one on their opposite side to center your image. I recently did it to center a label between another label and the the remaining 3 edges of the view

Scale vertically stacked buttons with Auto Layout

I have 4 buttons with UILabels under them. It looks good on the iPhone 6, but I'm having trouble getting the buttons to stay in the same configuration with Auto Layout. I've tried constraining the Aspect Ratio and the distances to the edges, but the aspect ratio still gets distorted. I deleted all the constraints for now.
What approach should I take?
The key is to use equal height constrains (red = orange, orange = blue, blue = purple) in conjunction with vertical spacing zero (same way between red and orange)
Pin the labels to the bottom (the white views in my example).
The leading and trailing of the buttons are also pinned to the borders of the superview to define the horizontal size.
Please follow the below steps to get your requirement done.
Give Each Button to label "Vertical Spacing" & "Center-Y".
The first button to it's superView "Top Space to Top Layout Guide".
The last button to it's superView "Bottom Space to Bottom Layout Guide".
set Each button to it's superView "Center Vertically in container".
Fix Buttons-Labels; heights & widths.
To achieve this , you have to add constraints for - leading space, trailing space, top space and aspect ratio for each element.
Constraint for Button:
Constraint for Label:

UIScrollView and Autolayout prevent contentview compression

In IB I have a view controller that contain a scrollview.
The red view is inside the scrollview
The minimum height of the red view is 504px, so on iphone 4 it should scroll and on iphone >=5 it should extend and layout the buttons to fill the blank.
I set the constraints of the red view to 0 from top, leading, trailing and bottom of the scrollview and also centered horizontally and vertically.
My redview has a minimum height of 504
The blue and green views have equal height
Everything is fine for iphone >=5 but for iphone 4 the red view is compressed to be the size of the scroll view and the buttons are touching each others.
After multiple constraints adjustments I'm wondering if it's even possible to do what I want 100% in IB with autolayout
You say (emphasis mine:)
"I set the constraints of the red view to 0 from top, leading, trailing and bottom of the scrollview and also centered horizontally and vertically. My redview has a minimum height of 504"
First, I can see some redundancy (the bold part).Your redView does not need the vertical constraint since you have already pinned it on the edges of the scrollview and has a defined minimum height.
But this could or not be the cause of the buttons coming closer together, depending on the rest of the constraints.
Have you set vertical space constraints for the buttons?

How can I give constraints some margin from the center of superView

I am using auto layout. I have one UIView that takes place at 20px margin from the center of the superview.
Right now I have given the Top Space to superview that i have make in the iPhone 6+ Xib. but when i run in the iPhone 4 simulator it goes to too much down.
Any idea
If you want the view to be placed relative to the center of the superview, you should be using centerX and/or centerY constraints, not a constraint to the top of the superview. In IB, if you choose the "Vertical Center in Container" constraint to center the view vertically, you can edit that constraint to change its constant value which will offset the view from the center by whatever number you choose.

Resources