Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSCollectionView Auto-Resize Subview with Auto-Layout

I am trying to achieve a stacked arrangement of simple NSViews, and for that I'm using an NSCollectionView bound to a Dictionary Controller. The result I have so far is below:

Normal

The labels on the left are bound to the controller item keys and the SegmentedControl selectedIndex to the item values.

Problem is, I have no idea how to force the subviews within NSCollectionView to conform to the container width; what happens is, they maintain their own width and when the NSCollectionView becomes too narrow horizontal scrollbar appears, like below:

enter image description here

I am aware of this question (and the "inverse", this one), and I've never worked much with Autolayout beyond trivial things to be honest. I am wondering, with all the fanciness that AutoLayout brings with it, is there a way of dealing with this issue without resorting to coding and using events/notifications? (I understand that observing NSViewFrameDidChangeNotification is an alternative, right?)

Thanks

like image 711
insys Avatar asked Nov 01 '22 07:11

insys


1 Answers

Just did something very similar - except using an NSSplitView subclass - so if I understand your question correctly you'd want to setup constraints for the subviews to attach to the sides of your superview like this:

NSDictionary * viewsDict = NSDictionaryOfVariableBindings(filler);
[self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[filler]|"
                                                             options:0
                                                             metrics:nil
                                                               views:viewsDict]];

You might also have to fiddle with the Content Compression Resistance Priority parameter of you subviews to achieve the desired results.

Also note that when targeting OS X 10.9 exclusively there's the new NSStackView class:

"NSStackView is a new class in OS X 10.9. It is used to layout horizontal or vertical stacks of views using auto layout. Necessary constraints will automatically be created and modified when adding and removing views from a stack view to maintain a cohesive layout."

like image 156
Jay Avatar answered Dec 15 '22 11:12

Jay