0

I have an NSCollectionView in one .xib and the subview collection view item for that collection view in another .xib

I am trying to add constraints to my collection view item in order to let it to

  1. Align the edges with the superview's view
  2. Resize itself according to the superview

The collection view item in this second .xib is a NSTableView with constraints as shown in the attachment.

Constraints configuration

Upon startup I get the following

  1. The superview, i.e NSCollectionView is larger than its subviews.
  2. If I resize the entire window and make it smaller, the NSCollectionView will resize itself too, but the subviews do not. See second attachment.

enter image description here

11
  • 1
    The items usually provide their size to the collection view, which then lays them out in a grid. It doesn't make much sense to have the items try to conform themselves to a size provided by the collection view. How would the collection view pick a size? That said, you can set a minimum and maximum item size on the collection view. Commented Jul 8, 2014 at 4:20
  • I see. It is possible to achieve this with NSScrollView? I know that NSCollectionView is embedded in a NSScrollView Commented Jul 8, 2014 at 9:45
  • Achieve what? How would you like the size of the items in the collection view to be determined? You can make them whatever size you want. Commented Jul 8, 2014 at 12:20
  • 1
    NSCollectionView won't do what you want. Given the fairly simple, single-row layout, you may be able to use an NSStackView. You'll be responsible for adding a view per represented object, but the stack view will lay them out horizontally. Vertically, you can set up constraints between the contained views' edges and the stack view's edges. Commented Jul 10, 2014 at 21:10
  • 1
    A scroll view won't do anything but provide scrolling for a content view that's larger than it. You could make the moral equivalent of a stack view by just creating a plain NSView as your container and then fill it with subviews. Set the layout constraints to keep the subviews arranged how you like within it and to keep the plain view wide enough to contain them all. Set the plain view to be as tall as the clip view and the subviews as tall as the view. Make sure the subviews are flexible in their height at least relative to the priority of the view-to-clip-view constraints. Commented Jul 11, 2014 at 6:40

2 Answers 2

2

NSCollectionView won't do what you want. The items provide their size to the collection view, which then lays them out in a grid.

Given the fairly simple, single-row layout you're trying to achieve, you may be able to use an NSStackView. If you don't want to require 10.9 or later, you could make the moral equivalent of a stack view by just creating a plain NSView as your container and then fill it with subviews. You would put this view into a scroll view. The container view would have constraints to keep its top and bottom equal to the clip view's. Its width would not be related to the clip view's. (I don't know if you want the container view's leading edge tied to the clip view's. That might require some experimentation.)

Set the layout constraints to keep the subviews arranged how you like within the container view and to keep it wide enough to contain them all. You'll want constraints to keep the tops of the subviews related to the top of the container view and similarly for the bottoms. However, make sure the subviews are flexible in their height at least relative to the priority of the view-to-clip-view constraints.

Sign up to request clarification or add additional context in comments.

Comments

0

I've been able to achieve what you describe with NSCollectionView by setting the itemSize equal to the collection view's width in the FlowLayoutDelegate while calling collectionView.collectionViewLayout?.invalidateLayout() in viewWillLayout in the view controller to handle resizing.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.