In one of the WWDC 2012 videos, Auto Layout By Example, they demo an OS X app using Autolayout, and at about 7 or 8 minutes in, he shows how for a single view, you can uncheck a box in the Attributes Inspector, and the box is called something like "Translates Autoresizing Mask Into Constraints". Now, I'm well aware of the code equivalent of this box, the translatesAutoresizingMaskIntoConstraints boolean, but I can't seem to find this checkbox anywhere in either iOS or OS X projects. My project uses Autolayout. I really would like this checkbox, because one of things I'm struggling with in learning Autolayout (and converting a springs/struts app to AL) is the million constraints Xcode has generated for each view, and how to clean them up and sensibly override some/all in code. What I'd like in order to do this conversion one view at a time is to turn off those auto-generated constraints.
Why can't I see this checkbox? I'm using Xcode 4.6.
The “Constrain to margins” checkbox determines whether constraints to the superview use the superview's margins or its edges. The lower portion of the popover lets you set the item's width or height. The Width and Height constraints default to the current canvas size, though you can type in different values.
translatesAutoresizingMaskIntoConstraints. A Boolean value that determines whether the view's autoresizing mask is translated into Auto Layout constraints.
That checkbox is available in Interface Builder (IB), but only if you are developing Cocoa projects targeted for OS X. For iOS, it's not available at present. You can only set it programmatically.
Auto Layout on iOS from my understanding - and others feel free to pitch in here - is not a full implementation of what is available on OS X.
To be honest, given what you say afterwards, this checkbox is probably something you don't need to worry about. I think it is important in upgrading OS X projects to Auto Layout, but generally for iOS it's unlikely you'll be mixing one and the other. I.e., you either checkbox your Xib in the File Inspector to "Use Autolayout" or you don't.
That said, there is one use case where you may need to mess with that flag. That's if you want to create a standalone Xib file for a view, and then load that programmatically using loadNibNamed
. When doing that, by default old style Auto Resizing constraints are converted into new style Auto Layout constraints. Typically I want to add my own so I set that flag to zap 'em.
myView.translatesAutoresizingMaskIntoConstraints = NO
Anyway that's another story.
Here's the link for more info, although you've no doubt had a look at it already:
Adopting Auto Layout
One thing I'd say is that if you're struggling with Auto Layout in the beginning - and you wouldn't be human if you weren't, we all have been - then I'd stick with Interface Builder and think about the golden rules. The most important one for me is that it hates ambiguity. It's like a vacuum in nature. Before you can delete the constraint that you don't want, you have to add the one that you do want then zap the old one.
The other mistake that I made was mixing Auto Layout and frames. So I'd do some code that checked the frame width then apply that to the constraints. Bad mistake. That really ends in tears. When you get into Auto Layout it's essential to really forget about doing anything with CGRect
, frame
, etc.
Stick with it though. Start with some simple views in IB and experiment. There is method to the madness, really.
One more link also worth looking at is:
10 Things You Need To Know About Cocoa Autolayout
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With