Search code examples
iosswiftautolayoutnslayoutanchor

How can I 'add' two NSLayoutDimensions?


Adding two NSLayoutDimension objects doesn't really make sense, but I'm trying to set the height of a container view to the height of two subviews with dynamic heights.

Here's a visual representation of what I'm trying to do, where dynamic view 1 and dynamic view 2 both have dynamic heights, and the container view sizes according to the sizes of the two dynamic views.

 ----------------
|                |
| dynamic view 1 |
|                |
|                |
| dynamic view 2 |
|                |
 ----------------

Programmatically, something along the lines of:

heightAnchor.constraint(greaterThanOrEqualTo: dynamicView1.heightAnchor + dynamicView2.heightAnchor).isActive = true

But this obviously returns an error. What's the best way to set up these constraints?


Solution

  • Using the example above, all I needed to add was a bottom constraint to dynamicView2.

    dynamicView2.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true