Swift version: 5.2
If you create any views in code – text views, buttons, labels, etc – you need to be careful how you add Auto Layout constraints to them. The reason for this is that iOS creates constraints for you that match the new view's size and position, and if you try to add your own constraints these will conflict and your app will break.
There are two solutions. First, don't add Auto Layout constraints to views created in code. Sound like a bad idea? That's because it is: like it or lump it, Auto Layout is something you want on your side. So, that leaves option two: tell iOS not to create Auto Layout constraints automatically, and that's done with this line of code:
yourView.translatesAutoresizingMaskIntoConstraints = false
SPONSORED Are you tired of wasting time debugging your Swift app? Instabug’s SDK is here to help you minimize debugging time by providing you with complete device details, network logs, and reproduction steps with every bug report. All data is attached automatically, and it only takes a line of code to setup. Start your free trial now and get 3 months off exclusively for the Hacking with Swift Community.
Available from iOS 7.0 – see Hacking with Swift tutorial 6
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.