How to achieve this layout with auto layout ? - ios

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

Related

Vertical/horizontal view in superview - storyboard iOS

I need to center align a view (Inner View) vertically and horizontally in superview (Main View).
Here's the view controller:
I have set the centerX and centerY constraints properly. But Xcode shows error for missing constraints as given here:
This is the current layout. I want to center align Inner View vertically and horizontally in the screen. Or is there any other way to achieve this
You are relying a little too much on center constraints.
Setting centerX and centerY on a subview does not determine the width and height of its superview, which is why you are getting layout errors.
Select your Sign Up View and add Leading and Trailing constraints of Zero - that will give Inner View a width, and then remove the Sign Up View.centerX = centerX constraint.
That should satisfy the X Position requirements.
Next, with your Sign Up View selected, add a Bottom constraint of Zero, and remove the Sign Up View.centerY = centerY constraint...
That should satisfy the Y Position requirements.
The important thing to remember is that you need a complete "chain" of constraints for each axis.
Here's a screen-cap of my constraints. Note that I constrained the Top of Sign Up View to the Top of avocado-signup + 56 (your actual image size is probably different):

Xcode How to create a square button(width equal to height) inside a Horizontal stackview

I have a requirement to create 3 square buttons at bottom of the page. All of them must be aligned and their size should be square.
So far, I have created a horizontal stackview and created autolayout constrains 50 from the left, 50 from the right, 10 from the bottom and set the stackview height as 60.
How can I create square buttons, since the stackview subview width is dynamic and determined at run time?
How can I set an equal aspect ratio for these three buttons?
Don't set the stack view height; let the content determine that.
You haven't said exactly how you want the buttons laid out, but I assume you want them equally distributed.
Put the buttons in a horizontal stack view
Constrain the leading, trailing and bottom edges of the stack view to the superview
Set the stack view's alignment to "Fill" and distribution to "equal spacing"
On each button, create an aspect ratio constraint between its width and its height of 1:1
If you want a minimum height for the buttons, set a >= height constraint on one of them
If you want a fixed height for the buttons, set a = height constraint on one of them
I think the only thing you are missing is setting the Distribution in Attributes Inspector on your Stack View. Set it to distribute equally as shown in the image below:
To set the height, you simply need to set a height contraint on each button. Click the tie fighter looking icon and set the height for each:

Need help to set autolayout

Am new to iOS & am facing it very difficult to set autolayout. Watched many videos to learn, but all of them giving solution to a specific problem. No video covers all base rules to set an UI object into it's place & with proper flow.
I came from Corona Background & used to set UI programatically very well. Am thinking here same way, but I think apple made it so difficult or people are not able to explain me properly.
Please see 2 images attached in this question & tell me rules to apply to achieve this UI. I request you people to please explain in the general manner so that my other screen can be completed using same rules.
Image 1: http://i.stack.imgur.com/MPE47.png
Image 2: http://i.stack.imgur.com/qEiCm.jpg
A really helpful guideline is
Every element should be able to figure out its position (x and y) and size (width and height).
Ensure that every element only has one way to figure out its position and size.
Remember that the autolayout of all the elements can influence each other.
The most used layout constraints are:
Top The space between the top of the view to another view.
Trailing The space between the right edge of the view to another view.
Leading The space between the left edge of the view to another
Bottom The space between the bottom edge of the view and another view
Width Assign a fix width to a view (Note that it can also be a percentage - aspect ratio)
Height Assign a fix width to a view (Can also be a percentage)
Center Horizontally Always align the view relative to the horizontal center of another view
Center Vertically Always align the view relative the the vertical center of another view
For example in your second image, say the yellow bar is a UIView called titleView.
Set the position of titleView by setting the top layout constraint to the container view. y position is set.
Set the leading constraint to the container view. x position is set.
Set the trailing constraint to the container view. The view's width will now stretch with the screen size. Thus width can now be determined.
Set height to 50. Height is set.
Now... If you also set the width of this view, it will cause the layoutConstraints to break, because you have redefined the width constraint. Some of the constraints will then be ignored.
Another example of how layoutConstraints might influence each other. Lets look at determining the y positions of the second image.
Say titleView has a top constraint to the container + height of 50.
currentCampaignView has a top constraint to the bottom of titleView. (Use vertical spacing) + equal height to titleView. (y + height can be calculated)
the 5 buttons have equal heights. Top buttons have Top space to Bottom of currentCampaignView. Centre buttons have Top space to bottom of top buttons. Bottom button have Top space to bottom of centre buttons.
startCampaignView has equal height to currentCampaignView. Top constraint to bottom of bottom button and Bottom constraint to container view.
Note that because views and buttons have equal heights, all are considered when determining the height. Thus it is very important that they are all interlinked and that the entire height that can be used is specified. In this case it is specified by the first element titleView that has a Top Constraint to the Container view (of which the height should be known) and the last element, startCampaignView, that has a Bottom constraint to the Container view. Because all the views in between are linked on y position and height, the view can work out what each view's height and y position should be.
One more example. (Your first image)
topLeftButton Set the top constraint to Superview. (y), Set the leading constraint to Superview (x), Set height = 100 (height), Set equal width to topRightButton (Note that we do not quite have the width yet, because the width of topRightButton can not be determined)
topRightButton Set the top constraint to Superview. (y), leading constraint to topLeftButton (will be used for x), Set trailing constraint to superview. Now the width of both buttons can be determined, because we have an external startX + endX and we know the two buttons touches each other and are equal widths. Thus the available space will be split to get the width of the two buttons.

Storyboard iOS - How to give top margin as a % of screen size instead of static value top pin margin?

I want to top margin auto dynamic according the size of the screen.Like aspect ratio work for height and width.
Same way I want top margin as a some % from top instead of static pixel value top pin margin.
One way to get proportional spacing is to link the view to the centre of its superview.
First, create a constraint that makes your inner view centered vertically in its container. Then, change the constraint's multiplier to something other than one so that it's positioned however far from centre you want it (0.2 or whatever for the top).

NSLayoutConstraint height equal to origin.x in storyboard

How do I create a layout constraint in storyboard that makes the height of my view equal to its origin.x?
I'm using the latest xcode and ios.
Also using swift, but the answer can be in Objective-C.
You can do it with a helper view:
Put an helper view on left, with these constraints:
left = superview left
height: doesn't matter (10 or something so you can debug it)
top: also doesn't matter
Set your main view with left from superview/top/width whatever you want.
Set constraints between these two views:
left of mainView = right of your helper view (horizontal spacing)
height of your main view = width of your helper view
something like this: (the blue is the helper...)
You can't relate a height to a spacing constraint (in IB anyway, I haven't tried it in code). One way that will work in IB is to make the height relative to self.view's width and the origin.x relative to the self.view's trailing (for a view that's the full width of the screen, like a controller's self.view, the value of width and of trailing are the same). So, in this example I made the height 0.2 times the superview's width, and the leading edge of the view be at 0.2 times the superview trailing.
You make the height constraint by selecting the view and the superview, and choosing equal widths. You can then edit that to change it to height for your view, and change the multiplier to what you want.
You make the origin.x constraint by dragging from your view to the left side of the superview, and then editing that to change it to trailing (from leading) for the superview.

Resources