Vertical/horizontal view in superview - storyboard iOS - 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):

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

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.

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.

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.

No fixed height in interface builder, constraint predicament

The screen I'm trying to create is very simple. I have two UIViews stacked atop one another. The top UIView, topView, has height of 40px, and the UIView below, botView, takes up the rest of the screen. However, sometimes topView should not appear and I want botView to take up the entirety of the screen.
What I've tried is setting width, leading/trailing space on both views. Then topView gets distance to top layout guide, botView gets distance to bottom layout guide, and then an additional constraint for vertical spacing between topView and botView. But this results in xcode yelling at me that I haven't set a height or y constraint on one of the views. Of course, I can't set a height constraint because I do not want either of them to be fixed height.
I'm sure there must be an elegant constraint solution here that I just can't seem to figure out. Thoughts?
If topView should always be 40px height, then add a constraint for the height of topView.
As for botView, you can keep the current constraint that says its top has to be correlated with the bottom of topView. But add another constraint for bottom view with a priority lower than required that says it should be 0px away from the top of its superview. This way, when you remove topView from the containing view, botView's constraint related to topView will be deleted, and therefore will satisfy the lower priority constraint.
And if you want to push back topView in, recreate the botView to topView distant constraint with required priority.
There are a couple of ways to do this. One option
In IB lay out your views. Then pin top view 0 px from the top, bottom, left, and right. Specify a height constraint of 40 px as well. To easily do this click on your view, then in the lower right hand corner click on the button that looks like | + |. Click on the dotted red lines to specify the constraints. See image:
Do the same for the other view. Don't specify a height for this view because it's height is dependent on the 40 px view for its height. Next find the height constraint for the 40 px view. You can can use the assistant editor to control drag an outlet to you view controller just like you would with a UIButton or any other IB element. When you want to hide your 40 px view set the variables constant property to 0. When you want to show it again set it back to 40.
self.topViewHeightConstraint.constant = 0;
The other option you could go with is to pin the top view 0 px to the top, left and right. Don't provide a bottom constraint but specify the height as 40 px. For the bottom view add add a left, right, and bottom constraint of 0 px. For the top constraint add a constraint of 40 px from the top superview. Again add an outlet for the most recent top constraint and when you want the view to be larger set that constraint to be 0.
An alternative: In IB, add a distance to top layout guide constraint for BOTVIEW (it will be 40...) Then click-drag this constant to the VC to create a NSLayoutConstraint property.see also here
Now, upon the condition that hides topView, you set self.myConstraint.constant = 0; This will force botView to resize to meet the condition of being 0 from the top.
You may have to clear your current constraints to get rid of errors, but this should give the desired behavior.
As for "topView" like Tom Ahh suggests, go ahead and give it a height constraint of 40 and pin it to the top as you're already doing.

Resources