Question

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

Was it helpful?

Solution 2

The solution is very simple in fact: Switch to a view-based NSTableView. NSCollectionView is not intended for the use described in the question. With NSTableView, you get the resizing behaviour by default and for free.

It also works whether you're using Bindings or Datasource, it doesn't really matter.

OTHER TIPS

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."

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