23

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.

bneely
  • 9,083
  • 4
  • 38
  • 46
bobsmells
  • 1,359
  • 2
  • 11
  • 22

1 Answers1

27

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.

enter image description here

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

Yves
  • 5
  • 4
Max MacLeod
  • 26,115
  • 13
  • 104
  • 132
  • Wow, thanks so much Max, for that detailed and clear reply. I'm still slightly confused as to why you say you might not need this while converting an iOS project. I just want it so that I can do a step-by-step partial conversion to AL. I mean, if it's not there it's not there, but it's one of those situations where I'm wondering why it would be explicitly left out of the iOS dev kit, and more to the point why other developers wouldn't be complaining...i.e. I'm wondering if I am missing the point somewhere... – bobsmells Jun 11 '13 at 01:36
  • probably because in iOS they expect you to use AL almost exclusively in IB, most likely using story boards. Loading stand alone view Xibs programmatically with loadNibNamed is probably considered by Apple to be much less common. How is your project set up btw? Do you have standard view controllers with associated views, or are you using loadNibNamed? – Max MacLeod Jun 11 '13 at 07:24
  • I have standard VCs with associated views. I'm using a third party component (iCarousel-https://github.com/nicklockwood/iCarousel) and this provides a hook into providing your own view for each panel, via a similar method to cellForRowAtIndexPath when dealing with tableviews. The problem is, the view is returned by me from that method, which means I never actually add it to its superview, which means I don't get a chance to add constraints. Obviously I could dig into the iCarousel code and do it that way, but surely I'm missing something...shouldn't i be able to use a framework as a black box? – bobsmells Jun 13 '13 at 09:09
  • I've just watched the whole video from this year's WWDC which basically shows the new XCode 5 doing exactly what I'm after, i.e. IB won't add constraints until you ask it to. I think I'm going to continue my learning process using that. Thanks Max, although I was probably not asking exactly the right question, you've answered the one I did ask well. – bobsmells Jun 13 '13 at 23:57
  • The `frame` really make me crazy when mix `AutoLayout` and `frame`.... I finally update the `frame` again at the `-viewDidLayoutSubViews` after animation end. – AechoLiu May 05 '15 at 08:43