IOS Storyboard Constraining Every Subview to bottom of screen - ios

This seems like it should be very simple, but I am having a terrible time trying to get every subview (they act like menu bars) constrained to the bottom of the parent view in the IOS storyboard.
There seems to be automatic constraints placed between the subviews that make them override the constraint I'm trying to set. I try to take these off, and then place the new constraints, but nothing seem to work as I loop over trying to reset constraints, over and over and over...
It should be very simple, set all of the uiviews to a particular width and length and set the distance between the bottom of the superview to be equal to 0. That's it.

The secret to happy constraint editing in interface builder is simple:
Don't do it
If that isn't possible, then:
Create your own constraints before trying to delete the system ones
Make judicious use of the pinning menu and editing constraints in the inspector
Don't drag and drop your views after you've added them - this causes interface builder to discard all constraints and set you up with a new set of "best guess" system constraints.
I've written more about this here but that's the gist of it.

Related

How to set up my constraint perfectly to the bottom in Storyboard

I am using AutoLayout in Storyboard. I have set translateAutoResizingMasksToConstraints to false. I have created a constraint like so:
The superView takes up the whole screen's frame. When I run the App, the bottom bar view doesn't appear. However, if in the picture I change constant to 50, it jumps up to about where it should be expected.
I have had a previous branch where I did not set it to 50 but due to some other constraints it appeared as expected. But it's not exact and I don't know why it's behaving this way all of a sudden. I've used the View Debugger feature and the bottom bar view straight up doesn't appear at all.
How can I set it so that the constraint has a constant of 0 and still appears? What might be a reason for this bug? Thanks!
When you are trying to add constraints to two items, you try an easier way by choosing two of them(even super view) like this:
Then click 'add new constraints' on the right bottom of storyboard/xib,
Then you can choose different types of align constraints as you like.
If you only choose one item to add new constraint, then align will be disabled cause it needs two items.
Try printing the bar's frame in view did appear, if you are not sure whether the bar is just outside the visible field. Have you set constraints to the sides and a height as well? As a rule of thumb you can say that a view needs 4 constraints before iOS knows exactly where you meant for the view to be placed.

Xcode view hierarchy debugger - "prototyping"

What does the word "prototyping" mean in the view debugger? It's used to describe a constraint that I didn't add. In IB, the view in question (the upper-left onion image) has an intrinsic width, and its right edge is anchored. It shouldn't need any extra horizontal constraints.
In IB I was designing with a view having simulated metrics set to "Freeform" size, the debugger is showing the view on a 6S. I'm guessing the answer lies in the difference between the two, but can't fathom what's going on.
The added "prototyping" constraint is pulling the onion out of place, towards the left screen edge. Why was it added?
The 'prototyping' constraints are added by XCode Interface Builder when you don't supply enough constraints for it to determine what the layout should be. If you plan to add constraints later at runtime, which it sounds like you're doing, you can add constraints in IB and set them to be removed at build time.
Just add a constraint for the view's leading edge to superview (even though you don't want it), select the constraint in IB, and check Remove at build time.

Update Frames & Constraints in Xcode (Interface Builder)

I have been learning Xcode and iOS and things are going well with coding but I am confused about a few options in interface builder.
It seems that when I add a contraint I can choose to "Update Frames" and then either "Items of new contraints" or "All Frames" but it seems that I don't always need to do this... sometimes I do and sometimes I don't - I don't think I truly understand the concept. It seems to be optional. When is the case to choose it or not?
Once a constraint is added, then should that be it?
There is also a context menu (Resolve autolayout issues) (3rd item on the bottom on interface builder, represented by a triangle between 2 vertical lines) which also allows me to Force Update of constraint or update of frame. Why is this needed ? I notice the options inside are usually disabled so I presume this becomes active when there are issues ?
I have tried searching the internet and I can't find anything specific; the more I read, the more confused I seem to get.
Also, a sanity check: a frame is the bounding box of every single control (view) not just the viewcontrol, so every view (control) as its own frame. Is this correct?
I'll try to explain the concept of constraints and storyboard representation.
Constraints - are used to determine the position of UIView elements inside your view. For example, the vertical distance from top of the view to UIView element top. When you add constraint using the storyboard it plays 2 roles:
Positioning the element inside a view when your app runs
Positioning the element in the storyboard representation itself
Update Frames - is used when you want to update storyboard display so that it fits the currently applied constraints. When the display of the storyboard does not match the constraints a warning will appear.
Add missing constraints / Update constraints - will create/change the constraints applied on UIView element to match what currently presented in the storyboard. Kind of the other way around from update frames.
This answer is not a replacement for Apple's Auto Layout Guide
You do not have to click on "update frame" every time you add a constraint, if you do, it will update the frame of the view to whatever it should be based on the constraints. Yes, every view has a frame and it is the rectangle relative to the views superview.
More often than not you'll probably click on update constraints (Shift + Command + =), this will allow you to resize views that are already constrained, and have the constraints updated to match.

Modifying a storyboard that is already laid out with auto layout

I am working through the CS193P Stanford iOS course and I am struggling with auto layout. I have laid out my calculator ViewController and set up all of my constraints. Now I need to add additional UI elements and I'm not sure how to got about it. In the storyboard editor in Xcode my existing UI elements are taking up the entire view controller. Do I need to just delete all of my existing constraints, move the UI elements around to their new positions and recreate all of the constraints or can this be done incrementally without having to recreate all of the constraints.
You can move and resize the controls with the constraints still in place. Xcode will note that there are misplaced views, but that will go away after you adjust the constraints.
Depending on exactly what you want to do, you can tell Xcode to adjust the constraints to match the new layout on the canvas. However, if you need to interpose a new control between things that were previously spaced relative to each other, you should delete that constraint and add two new ones. That is, if you had something like (in visual format language) [view1]-[view2] and you want to put another view between those to get [view1]-[view3]-[view2], you will want to remove the constraint between view1 and view2 and create two new constraints, one between view1 and view3 and another between view3 and view2.

Can I create one layout constraint in interface builder and then apply it to multiple controls

Can I create one layout constraint in interface builder and then apply it to multiple controls?
I mean just use interface builder and don't write code.
Now the interface builder workflow is dragging some controls to a viewcontroller firstly and then apply constraints to them. But some constraints like width ,height,leading and trailing space to their superview can be applied to multi controls.Change that constraint will effect all the controls that applied it. Just like applying css to html component.I think if xcode's interface builder can provide such function, maybe in sometimes it's more effective.
You can't apply the same constraint to multiple views. However, you can achieve "cascading" constraints (so you can adjust one thing in interface builder, and everything else moves) by creating your constraints carefully.
Say you have a stack of buttons, which you want to be all the same width and left / right aligned. Create a constraint on the top button pinning it to the edge of the superview, and another one, giving it a particular width.
For the other buttons in the stack, don't create width and position constraints, but simply pin the left and right edges of the button to the left and right edges of the button above. This way, the whole stack will follow whatever changes you make to the top button.
Another alternative is to use container views, and have your position and sizing constraints on the container view and just make the subviews pin to the edges. The best approach depends on the final layout you are trying to achieve.
You might want to read more on auto layout. You cannot create all possible constraints in IB. Constraints are objects but you cannot change what views they are related to.
They are not mutable.
You can adjust the constant and the multiplier at run time.
No, you can't do that in Interface Builder. You should learn the Auto Layout Visual Format Language, which does let you apply similar constraints to multiple views in one pass.
Here's the official guide.
This blog post has a great introduction, and many helpful examples.

Resources