views:

33

answers:

1

Couldn't find anything on the net about this; and wondered if anyone on SO has a solution.

I have an NSView with several subviews that are centered by removing the left and right anchor points. When I resize my view, programatically or with the mouse, to a smaller width than the subviews: it pushes them off center. Has anyone come across this before and do you have a solution?

EDIT: I want to be able to resize my view to a zero width. The reason being, the view is actually part of a split view and I have hooked up a button to 'collapse' it. When it collapses all of the subviews are pushed off-center and aren't re-centered when the view is resized, effectively un-collapsing it.

A: 

I have solved my problem now and thought I would share incase anyone comes across this issue in the future.

No amount of playing with autosizing options or view layouts in Interface Builder seemed to stop my subviews from getting moved off center. I did manage to find this link here and from this page, the advice:

Springs and struts, as currently implemented, are really no good for anything but keeping either one or both sides of a view "stuck" to the nearest edge. Any sort of centering behavior, division of gained/lost area between multiple views, etc. has to be done by hand.

Based on this I overrode my view's drawRect: method and manually laid out my subviews using their setFrame: method. This works great and gives me the results I'm looking for.

It does feel clumsy though and that I shouldn't have to override drawing behaviour for something as simple as keeping subviews centered. If anyone has any better ideas for this I would love to hear them.

Greg Sexton