5

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

1
  • Were you able to get this to work? I wonder if there is a way to do it from IB.
    – tofutim
    Feb 25, 2014 at 18:53

2 Answers 2

1

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

2
  • 1
    Thanks for the answer. I totally forgot to mention that I'm using bindings (edited question), and that's one of the reasons I want to keep using NSCollectionView (it's really easy to use). I played a bit with the Content Compression Resistance Priority, setting it to a lower value ( < 500, as described here ), but nothing changed... it seems for CollectionView's children different rules apply?
    – insys
    Jan 8, 2014 at 19:46
  • Is there a way to set this up from IB?
    – tofutim
    Feb 25, 2014 at 16:42
1

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.

2
  • I mean iOS self.tableView.rowHeight = UITableViewAutomaticDimension;. Is there any alternative for macOS? Sep 26, 2016 at 15:23
  • Umm what you're asking is a totally different beast.. I'd suggest specifically asking for that.
    – insys
    Sep 26, 2016 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.