So let’s dive into the meat of the whole module. We’ve nailed the whole theory by now and you know what to expect from a theory standpoint. Constraints are the part that make the App adapt to layout changes and they are the part that creates the magic in our apps.
From a technical standpoint, you know what’s coming. However what we haven’T looked at is how the implementation is handled.
Basically there are two ways to make use of constraints in your Xcode projects. You have the option to create them inside of the Interface Builder, or you could make use of the constraints by defining and modifying them in code.
They don’t have to be mutually exclusive, but it helps to decide on one approach and stick to it because otherwise it’s hard to keep track of what’s happening in your layout. There might be some bugs awaiting if you ignore this fact.
Let me just state here that I don’t see any issue in mixing the two concepts if you take a couple of things into consideration. For example, I recommend you define the constraints in Interface Builder. We as humans rely greatly on our visual sense to collect information and the Interface Builder has some excellent ways to visualize the constraints and make them easy to grasp and understand.
On the other hand, there are some things that need to be done in code – for example when dealing with animations – and there is no way around connecting and modifying them in code. So my recommendation when you’re just starting out with auto-layout, is to only make use of the code to modify constraints in the beginning.
The visual way of creating constraints inside of the interface builder helps understand whats going on. However if your apps get more complex or you work on projects with multiple team members, handling merges on git can become painful. That’s usually when you would want to switch to code entirely, but right in the beginning the interface builder is the way to go as it makes grasping the concepts super easy.
So in summary:
- Create constraints using the Interface Builder
- Modify constraints at runtime using code
Now that we’ve got that out of the way, let’s dive into the Interface Builder.