UIView.translatesAutoResizing is false in viewDidLoad(), true in viewDidLayoutSubviews()

117 Views Asked by At

I have a plain old UIView, created in IB, so its translatesAutoresizingMaskIntoConstraints flag should be set to false. And sure enough, it is — in viewDidLoad(). But by the time viewDidLayoutSubviews() rolls around, the value has morphed to true. And of course the constraint generated by the translation conflicts with one of my own constraints.

How or where is this flag being changed?

The upshot is I get the beloved "Unable to simultaneously satisfy constraints" console output. But changing the flag back to false in viewDidLayoutSubviews() fixes the conflict. So I know what to do, but I don't see why. (Yes I have cleaned the project and restarted Xcode.)

1

There are 1 best solutions below

0
Andrew Duncan On

Apple docs make clear that when the Storyboard is set to use Auto Layout, and one of its Views has no constraints, iOS contrives to give it some constraints. The surprise is the chosen mechanism: the OS automagically sets the translatesAutoresizingMaskIntoConstraints flag to true right before layout.

It's an approach I might have used too: by flicking one switch, the OS assures that the auto-generation of constraints takes the same code path for IB-created Views as for programmatically-created Views. (Which have the flag set to true by default.)

In my case, I was clearing the pre-existing constraints in code before programmatically creating new ones. But I guess the magic re-setting of the translatesAutoresizingMaskIntoConstraints flag happened in between those two actions.