How to Add Dynamic Constraints to Your Swift Project
Tags: macos, xcode, interfacebuilder, apple
Published: 2024-04-08 20:44:52

_I just wanted to start out by saying that this is an abridged version of my [original published version from June 9, 2021 that you can find on my website here](https://www.simplykyra.com/how-to-add-dynamic-constraints-to-your-swift-project/) and at that time I was running Xcode version 12.5 (12E262). I have since switched to primarily SwiftUI but figured I'd still share in case it can help you._

---

**_My Problem_**

At the time I originally posted this I had been working on a MacOS prototype application and was playing around using the Interface Builder with `IBOutlets`. There I used a label to display an error message that was conditionally visible or not based on if the user should see that message. 

My issue was with the space where the message went. It looked great when visible but when hidden the blank space was still there and didn't shrink down. You can see that space here marked in purple here:

![Purple square showing the empty space where the message would be if visible](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ijhks80wk1vq12xtidct.png)

I finally realized the Interface Builder attributes I was adjusting wouldn't be helpful when I noticed the checkbox `Remove at build time`.

![Shows the vertical spaces constraint I was working on](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/paglonllejlvnjnlm8dz.png)

I checked it off and decided to explore a more programmatically solution.

**_My Solution_**

To create the constraints programmatically I knew I needed to access all three of the following controls in question within my back end code and alter which control the bottom `NSScrollView`/`textview` was constrained with.

![Shows my controls simplified](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/11aas0m22rgj2fi46f6f.png)

After the outlets were connected I next instantiated two new `NSLayoutConstraints` right after my outlets:

![Instantiation of the three IBOutlets and two constraints](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7har4sskfh26cy5znb2h.png) 

Next I went to the `viewDidLoad` function and added:

![The constraints were set and isActive was set to true](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yhwye4t32w768ng1wxc8.png)
  
Finally I went to where I controlled the visibility and `stringValue` of the error message and added the following code to set which constraint was active or not:

![Shows the constraints set to isActive true or false](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qj6zbxwyuyp1smhykr1r.png)

The order of this does matter as if the last two lines were reversed I received an error when it was run.

**_It Works_**

With the constraints declared, instantiated, and activated (or inactivated) it’s time to test this out. And the space disappeared when the error message was hidden!


![By default, on load, I activated the constraint that sets the distance between the stack view (where the two checkboxes and the button is) and the scrollable text view (the white square below). This means the label appears to have never been there. Looking great so far!](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/agc3fg5ca883hmdx8y7v.png)


![After running the code that results in an error message I was happy to see that the scrollable text view was moved down and the label was set and visible. After this screenshot was taken I ran the code with no error message which resulted in the scrollable text view moving up and the label once again disappearing just like the previous image. So overall it works great!](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/y618n2piu520qa4gc7z3.png)

I hope this helps you and your coding is going great!

---

If you want more information about my problem, the solution, or even to come along with me on my search to discover it you can check out [entire published version from June 9, 2021 on my website here](https://www.simplykyra.com/how-to-add-dynamic-constraints-to-your-swift-project/)!