Full Width NSCollectionViewFlowLayout with NSCollectionView

I have an NSCollectionViewFlowLayout that contains the following:

 - (NSSize) itemSize { return CGSizeMake(self.collectionView.bounds.size.width, kItemHeight); } // End of itemSize - (CGFloat) minimumLineSpacing { return 0; } // End of minimumLineSpacing - (CGFloat) minimumInteritemSpacing { return 0; } // End of minimumInteritemSpacing 

I tried ways to respond to the layout (set the width every time I resize). I tried to add the following:

 [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(onWindowDidResize:) name: NSWindowDidResizeNotification object: nil]; - (void) onWindowDidResize: (NSNotification*) notification { [connectionsListView.collectionViewLayout invalidateLayout]; } // End of windowDidResize: 

And this works fine if I expand the collection view (larger size). But if I try to collapse the view (resize smaller), I get the following exceptions:

 The behavior of the UICollectionViewFlowLayout is not defined because: The item width must be less than the width of the UICollectionView minus the section insets left and right values, minus the content insets left and right values. The relevant UICollectionViewFlowLayout instance is <TestListLayout: 0x106f70f90>, and it is attached to <NSCollectionView: 0x106f76480>. 

Any suggestions on how I can solve this?

NOTE 1: This is macOS, not iOS (although the error message says UICollectionViewFlowLayout).

NOTE 2: Although I get a warning / error, the breadth of the layout works, but I would like to find out the main problem.

+8
source share
2 answers

The source code that I posted in the question works fine with macOS 10.14 without any problems. I added the following to my window that displays the collection.

 // Only Mojave and after is resizable. Before that, a full sized collection view caused issues as listed // at https://stackoverflow.com/questions/48567326/full-width-nscollectionviewflowlayout-with-nscollectionview if(@available(macOS 10.14, *)) { self.window.styleMask |= NSWindowStyleMaskResizable; } // End of macOS 10.14+ 
0
source

This is because you are using the didResize event, which is too late. Your items are too wide the moment the window starts to shrink. Try using:

 func shouldInvalidateLayout(forBoundsChange newBounds: NSRect) -> Bool { return true } 

... when overriding the layout of the stream so that everything is recounted.

0
source

Source: https://habr.com/ru/post/1275115/


All Articles