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

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.

Related

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

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

auto layout stretching image issue

I'm trying to stretch an image from the very left of the view to the very center. I tried pinning constraints using left 0, bottom 0, and right 300, but it doesn't exactly goto the center. Any ideas?
Assuming that you know how to set the left, top, bottom, height constraints I will explain you how to set the right constraint, which will cause the imageView to span from the left to the center of the screen. Setting any width constraint will not work, because the width depends is different on every device.
click your imageView
choose Align -> Horizontally center in container
click on the newly created constraint
choose the Size Inspector
Change the First Item from Center X to Trailing
The "standard" Horizontally center in container causes the Center X of your view and its super view to be aligned. If you change the First Item to Trailing instead of Center X you therefore align the trailing edge / right of your view to the centerX of your superview.

Unable to get the constraints right in xcode storyboard

I was playing around with iOS 8 auto layout and constraints and ran into this simple issue, and wondering if someone can help me.
All I want to is to create view (uiview) and have a label at the bottom center.
So I created a new project, Main story board is set to wAny hAny.
Dragged a view into it
x: 16, y:20
Width 275, Height:560
Color : Green
Added the following constraints
Pin top space to superview
pin leading space to superview
Pin hight
Pin width
Added a label and centered it to the bottom of the view
on the label, pinned leading space to superview
pinned bottom space to superview
Currently not seeing any constraints error
using iPhone 6 in the simulator
in portrait mode, I can see the full view and the label at the bottom
Now rotated to the left which is now the screen is in landscape mode. but the view is still on the left side of the screen vertically and 1/2 of it is cut.
So my question is how can I fix this issue so the view also rotate or I can the whole view with the label?
Generally you don't want to be pinning to one side and not the other because as the view size changes (such as with rotation) only one side of the view will get pulled, and that's exactly what you're seeing.
So, either you should not pin and instead centre horizontally in the superview (and allow the intrinsic content size of the label to set the size) or you should pin both leading and trailing (so the label will always be resized to full width).

Auto-layout a basic thing [duplicate]

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.

Resources