Question

Being a somewhat proficient iOS developer, I have just started working on a desktop OSX project in Cocoa and I'm running into issues that I just can't grasp. So this question is for the OSX developers out there.

I don't like the Interface Builder much, so I tend to write my views in code. The most prominent method I write my view layout code in is a view controller's loadView method, and at least on iOS I use autoresizingMasks for everything. Try out the view small, large, rotated landscape and portrait and if all is dandy, I continue with the next item on my list. Now on the desktop, the autoresizingMask works (or just looks) a little bit different. First of all the properties have different names, but their behavior also seems weird or unexpected.

When I ran into the issue below, I thought it must be my code was wrong, so after trying out long enough I re-created it with Interface Builder just for confirmation's sake, and guess what: I got the exact same result. Take a view with four vertically stacked subviews. Set the middle two to have flexible heights, the outer ones to be fixed. When you run it, size it down and back up again, I get two completely different layouts before and after the resize. See image:

enter image description here

Now I can follow why this happens from a mathematical standpoint between run loops, but from the point of an 'autosizing' or 'autoresizing' feature, this makes absolutely no sense.

Before I try to write the mother-of-all-resizing-topics here, might I ask you these questions? Feel free to elaborate some more on the resizing topic if you feel it adds to the post.

  1. Am I a fool for not wanting to use the Interface Builder on desktop projects?
  2. Should I depend on the autoresizingMask less than I would on iOS projects?
  3. What are decent alternatives to making sure your layout lives up to standards without Interface Builder?

Cheers!

Was it helpful?

Solution

  1. Yes, in my opinion. :)

  2. You should depend on it when it does what you need. When it's insufficient, override resizeSubviewsWithOldSize: and/or resizeWithOldSuperviewSize: (or see below).

  3. ???

If you can target 10.7, look at the new constraint-based layout system. Check out the Cocoa Autolayout video from WWDC 2011.

You could also set minSize on your NSWindow to something large enough to prevent the singularity.

OTHER TIPS

  1. I'm not sure I'd say "fool," but refusing to use Interface Builder on the Mac is a very…avante-garde choice.

  2. You should definitely use autosizing on your views.

  3. Be maniacally attentive and spend lots of time making sure everything is right. (This is why I don't recommend going without Interface Builder. In general, what you get is a lot of wasted time that you could have spent doing something else.)

In this case, I think the best approach would be to set a sensible minimum height for the window. Don't let it get too small to display what it needs to display.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top