Update Frames & Constraints in Xcode (Interface Builder) - ios

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.

Related

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.

iOS Swift, Constraint pushing view off sotryboard

I'm working on my first swift app and I found out quickly that I need to use constraints to properly layout my page. So far I'm running into two issue.
The first one is that after adding some constraints, my view is pushed off the screen on the storyboard, but it looks fine(outside of my second issue) in the Assistant editor.
My second issue is that on some phone sizes the constraints are causing the text fields to expand when viewed in the Assistant editor and on an iPhone 6s test device.
I've added some screen shots below. Any help with this issue would be really appreciated.
Each time you add a constraint in storyboard, it is not automatically applied to your working view. You may think it should be applied immediately, but try to think think like this. If you have a view, and you apply height constraint (for example) and if Xcode applied it automatically you would get a view of width 0, since you haven't applied width constraint. That's why you need to tell Xcode to update constraints explicitly.
So when you want to update constraints, select view you want to update and go to Resolve Auto Layout issues, and click on Update Frames. This is located in the bottom right corner of your storyboard. It looks like a small triangle between two lines. Here you can update all views or just selected views.
For the second issue, you need to apply fixed width constraint to your stack view, instead of adding leading and trailing constraints. By doing that stack view will have the same width on each device.

Xcode 6.3 Storyboard Autolayout Constraints

This is my storyboard:
This is the iPad Simulator:
For the constraints I go to the bottom right and click on the button that has a triangle with two extruding bars (it sort of looks like a star wars fighter with a triangle as the cockpit). I scroll down to the section that says all view in view controller I click on clear constraints. Then I go back to the same button but this time I click add missing constraints.
Why is this happening? How can I fix it?
Adding missing constraints doesn't mean "add the constraint I think will look better in my project". Xcode adds some constraints to keep everything in place, but this may (or may not) be what you want.
To paint a control in place without ambiguity your need at least 4 constraint, two on each axe (two for X, two for Y). I recommend to clear all constraints, and start control by control, adding constraints.
Some tips:
you can set the constraints, then move a control, ask Xcode to refresh the constraints to the new position with the update Constraints option. update frames option do the opposite: given a set of constraints, repaints your control in the right position.
Use the Assistant editor to split your Xcode view, so you can select Preview and have while designing your Storyboard and see how it looks. You can add different devices to see how it fits (little + button at the bottom)

How to set in Xcode Interface Builder constraints so that views change their width or height accordingly?

I'm using Xamarin Studio for iOS development and I've just started to use the Xcode Interface Builder and it is giving me headaches. My Xcode version is 5.1.1(5B1008) and I'm on Xamarin Studio 5.5 build 227 with Xamarin.iOS 8.2.0.193 on Starter Edition.
I'm not able to understand how to use the Auto-Layout functionality and I've read tutorials, most say the say, drag element into view, click on Pin icon, set constraints and be done with it.
But the elements I add don't end up looking as I want them to be.
For example let's say I want to create a view with a scroll view in it and that will display labels and text fields in it as if it were a form, by dragging-dropping element imagine I end up with something like this:
The layout I desire is for every label and text field to be 11 dps apart from the left and right, no matter the orientation, also each needs to be 11 dps apart from each other, with the initial label to be 50 dps apart from the top of the scroll view.
Every element should be the same height (20 dps) always but their width must change according to the left and right constraints.
That's the end of my layout definition.
Now let's start with defining the left and right constraints along with the height. What I do is to select all of the label and textfields, I pin the left and right and set their height to the same value:
But what I get looks like this:
The resulting layout looks nothing like I wanted, the width for every element is set to the same size and the right values don't make sense, I stated 11 dps but I get values like 187, 173 and 228.
Even if I set the top and bottom constraints, the right constraints keep looking the same:
How do I fix this? I've deleted and started from scratch several times but I can't find to understand what am I doing wrong. Some of the technical language challenges me sometimes (I'm not a native english speaker) so I don't know if I'm interpreting something differently.
Some of the links for the tutorials I've looked into are this, this and this.
You need to ask Interface Builder to update the frames of those elements. All those yellow lines/numbers are constraints that are currently not satisfied - the frames of the elements don't match what the constraints require.
However, it's often better to make sure you have all your constraints set up first, before asking IB to update the frames.
Here, I've set up all the constraints you described, but I haven't asked IB to update the frames:
Here, I've asked IB to update the frames of all views in the top-level view:
If I select the subviews, I can see that the constraints are now shown in blue, meaning they are satisfied:

IOS Storyboard Constraining Every Subview to bottom of screen

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.

Resources