Where to update Autolayout constraints when size changes?

autolayout, ios, rotation, swift, uiview

Solution

There is no reason to update the width manually:

- Place all the views with equal width in your view with no spacing in between each other

- Add an equal width constraint to all of them

- Add constraints with 0 width for spacing between sides and each other

- Lower the priority of one or more of the equal width constraints just in case the width cannot be divided equally.

Then auto layout will handle everything for you.

Problem

I have several `UIView`s laid out along the bottom of a containing `UIView`. I want these views to always be equal width, and always stretch to collectively fill the width of the containing view (like the emoji keyboard buttons at the bottom). The way I'm approaching this is to set equal widths to one of the views, then just update the width constraint of that view to be `superviewWidth / numberOfViews` which will cause all of the other views to update to that same value. I am wondering where the code to change the constraint constant needs to go. It needs to be set before the keyboard appears on screen for the first time and update when rotating the device. My first attempt at a solution was to place it in `updateViewConstraints` and calculate the width via `containerView.frame.size.width`. But this method is called twice upon load, the first time it calculates the values correctly, but the second time for some reason the `containerView`'s width is 0.0. Another issue is that when rotating, the `containerView`'s width is not the value that it will be after rotation, it's the current value before rotation. But I don't want to wait until after the rotation completes to update the constraint, because the buttons will be the original size then change which will be jarring to the user. My question is: where is the most appropriate place to put this code? Is there a better way to calculate what the width will be? I can guarantee it will always be the exact same width as the screen width. And I am using Size Classes in Xcode 6, so `willRotateToInterfaceOrientation` and similar methods are deprecated.

Original source