Kenny Pederson
Kenny Pederson

Reputation: 31

Update the size of an NSCollectionViewItem when its parent NSCollectionView changes size

I have an NSCollectionView which has NSCollectionViewItem's as child objects. I have the flow layout of the NSCollectionView set the width of the items to fill the width of the view when the application is launched, but if I resize the window, the items stay the original width and no longer fill the view. Is there some way that I can have this resizing (fill width) happen automatically or is there a method that is called when the collection view is resized (from window resize) that I can access to set the width of the items to fill the view?

Upvotes: 2

Views: 1279

Answers (1)

Aleksey Gureiev
Aleksey Gureiev

Reputation: 1759

All you have to do is to override the collection view class like this:

class MyCollectionView: NSCollectionView {
  override var frame: NSRect {
    didSet {
      collectionViewLayout?.invalidateLayout()
    }
  }
}

This ensures that whenever the frame of the collection view changes, it invalidates the layout.

Upvotes: 6

Related Questions