NSToolbarItem and Auto Layout

I am in the process of converting the user interface for a rather old application to use Auto Layout. In most case, this works brilliantly, and I get perfect layout in every locale. However, I have run into a snag when trying to convert my toolbars to also use Auto Layout.


The toolbar is programmatically created and, among other things, contains an item that has a custom view defined in the xib file owned by the window controller. After several hours of experimenting, I haven't managed to figure out how to get Auto Layout to have correctly for this custom view. If I check "Translate Masks into Contraints" for the view, then it never resizes itself, so that in some situations its content gets squashed even though I set its compression resistance priority to 750. This is the case even if I make sure that the autoresize mask allows the view to resize itself. If I uncheck that checkbox on the other hand (which seems to be the correct thing to do), then I get a "Detected missing constraints" error at runtime. Also, the content of my view then resizes itself correctly but, whenever it expands, it ends up being clipped.


So it looks like the toolbar sticks my view into a container view and, in order to get the correct behaviour, I should set up layout constraints that allow my view to position itself correctly within that container. However, I don't see how to access that container view and at which stage to set up these constraints... Any idea what I am doing wrong? I am probably missing something obvious since one would think that this is a very standard setup, but Google didn't come up with anything.

Accepted Reply

OK, let me reply to myself just for the record. I eventually asked Apple about this and it was classified as a bug.


One hack that provides the desired behaviour is to uncheck "Translate Masks into Contraints", but to then listen for size changes and to adjust the size of the NSToolbarItem "by hand" whenever the custom view resizes itself. It is not perfect since it still throws out a warning about missing constraints, but it seems to work fine.

Replies

OK, let me reply to myself just for the record. I eventually asked Apple about this and it was classified as a bug.


One hack that provides the desired behaviour is to uncheck "Translate Masks into Contraints", but to then listen for size changes and to adjust the size of the NSToolbarItem "by hand" whenever the custom view resizes itself. It is not perfect since it still throws out a warning about missing constraints, but it seems to work fine.