代码
NSScrollView *_scrollView = [[NSScrollView alloc]init];
[_scrollView setBorderType:NSNoBorder];
[_scrollView setHasVerticalScroller:true];//垂直
[_scrollView setHasHorizontalScroller:false];//水平
// [_scrollView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
[self.view addSubview:_scrollView];
[_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.top.bottom.equalTo(self.view);
make.width.equalTo(@100);
}];
_scrollView.backgroundColor = [NSColor greenColor];
_collectionView = [[NSCollectionView alloc] initWithFrame:CGRectMake(0, 0, 100, 200)];
_collectionView.dataSource = self;
_collectionView.delegate = self;
_collectionView.backgroundColors = @[[NSColor redColor]];
NSCollectionViewFlowLayout *flowout = [[NSCollectionViewFlowLayout alloc] init];
flowout.minimumLineSpacing = 0;
flowout.minimumInteritemSpacing = 0;
flowout.itemSize = CGSizeMake(100, 40);
flowout.scrollDirection = NSCollectionViewScrollDirectionVertical;
_collectionView.collectionViewLayout = flowout;
[_collectionView registerClass:[CuMainTabItem class] forItemWithIdentifier:@"CuMainTabItem"];
// [[_scrollView contentView] setPostsFrameChangedNotifications:true];
_collectionView.wantsLayer = true;
_scrollView.documentView = _collectionView;
创建一个继承NSCollectionViewItem的类 和XIB;
遇到的问题
2023-02-16 11:18:52.982603+0800 CuTools[52408:2361076] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[NSNib _initWithNibNamed:bundle:options:] could not load the nibName: CuMainTabItem in bundle NSBundle </Users/liukuiqing/Library/Developer/Xcode/DerivedData/Mac_CuTools-hkaraygpxvjonadwblykrbkducpx/Build/Products/Debug/CuTools.app> (loaded).'
*** First throw call stack:
(
0 CoreFoundation 0x00007ff80e73d3eb __exceptionPreprocess + 242
1 libobjc.A.dylib 0x00007ff80e289e25 objc_exception_throw + 48
2 CoreFoundation 0x00007ff80e73d250 +[NSException raise:format:] + 214
3 AppKit 0x00007ff811868bfe -[NSNib _initWithNibNamed:bundle:options:] + 650
4 AppKit 0x00007ff81186892d -[NSViewController _nibWithName:bundle:] + 173
5 AppKit 0x00007ff81225af15 -[NSCollectionViewItem _nibWithName:bundle:] + 101
6 AppKit 0x00007ff811868551 -[NSViewController loadView] + 131
7 AppKit 0x00007ff81186836e -[NSViewController _loadViewIfRequired] + 72
8 AppKit 0x00007ff811d1e626 __24-[NSViewController view]_block_invoke + 27
9 AppKit 0x00007ff81185f302 NSPerformVisuallyAtomicChange + 132
10 AppKit 0x00007ff8118682e5 -[NSViewController view] + 123
11 AppKit 0x00007ff81212170c -[_NSCollectionViewCore _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:] + 1130
12 AppKit 0x00007ff812122245 -[_NSCollectionViewCore dequeueReusableItemWithReuseIdentifier:forIndexPath:] + 72
13 AppKit 0x00007ff8119fe55a -[NSCollectionView makeItemWithIdentifier:forIndexPath:] + 88
14 CuTools 0x000000010958efe1 -[CuMainViewControl collectionView:itemForRepresentedObjectAtIndexPath:] + 97
15 AppKit 0x00007ff8119fe447 -[_NSCollectionViewDataSourceAdapter collectionView:itemForRepresentedObjectAtIndexPath:] + 438
16 AppKit 0x00007ff8121152ea -[_NSCollectionViewCore _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:isFocused:notify:] + 100
17 AppKit 0x00007ff812115280 -[_NSCollectionViewCore _createPreparedCellForItemAtIndexPath:withLayoutAttributes:applyAttributes:] + 31
18 AppKit 0x00007ff81211844e -[_NSCollectionViewCore _updateVisibleCellsNow:] + 4276
19 AppKit 0x00007ff81211c28a -[_NSCollectionViewCore _layoutItems] + 271
20 AppKit 0x00007ff8119efdb0 -[NSCollectionView layout] + 343
21 AppKit 0x00007ff812149384 ___NSViewLayout_block_invoke + 595
22 AppKit 0x00007ff81185f302 NSPerformVisuallyAtomicChange + 132
23 AppKit 0x00007ff811866155 _NSViewLayout + 65
24 AppKit 0x00007ff81213d910 __36-[NSView _layoutSubtreeWithOldSize:]_block_invoke + 341
25 AppKit 0x00007ff81185f302 NSPerformVisuallyAtomicChange + 132
26 AppKit 0x00007ff81186610d -[NSView _layoutSubtreeWithOldSize:] + 75
27 AppKit 0x00007ff81213da7e __36-[NSView _layoutSubtreeWithOldSize:]_block_invoke + 707
28 AppKit 0x00007ff81185f302 NSPerformVisuallyAtomicChange + 132
29 AppKit 0x00007ff81186610d -[NSView _layoutSubtreeWithOldSize:] + 75
30 AppKit 0x00007ff81213da7e __36-[NSView _layoutSubtreeWithOldSize:]_block_invoke + 707
31 AppKit 0x00007ff81185f302 NSPerformVisuallyAtomicChange + 132
32 AppKit 0x00007ff81186610d -[NSView _layoutSubtreeWithOldSize:] + 75
33 AppKit 0x00007ff81213da7e __36-[NSView _layoutSubtreeWithOldSize:]_block_invoke + 707
34 AppKit 0x00007ff81185f302 NSPerformVisuallyAtomicChange + 132
35 AppKit 0x00007ff81186610d -[NSView _layoutSubtreeWithOldSize:] + 75
36 AppKit 0x00007ff81213da7e __36-[NSView _layoutSubtreeWithOldSize:]_block_invoke + 707
37 AppKit 0x00007ff81185f302 NSPerformVisuallyAtomicChange + 132
38 AppKit 0x00007ff81186610d -[NSView _layoutSubtreeWithOldSize:] + 75
39 AppKit 0x00007ff81213e718 __56-[NSView _layoutSubtreeIfNeededAndAllowTemporaryEngine:]_block_invoke + 1170
40 AppKit 0x00007ff81185f302 NSPerformVisuallyAtomicChange + 132
41 AppKit 0x00007ff811865c38 -[NSView _layoutSubtreeIfNeededAndAllowTemporaryEngine:] + 72
42 AppKit 0x00007ff81185f302 NSPerformVisuallyAtomicChange + 132
43 AppKit 0x00007ff811865be7 -[NSView layoutSubtreeIfNeeded] + 65
44 AppKit 0x00007ff8123e9bc0 -[NSWindow(NSConstraintBasedLayoutInternal) _layoutViewTree] + 148
45 AppKit 0x00007ff8123e9d80 -[NSWindow(NSConstraintBasedLayoutInternal) layoutIfNeeded] + 251
46 AppKit 0x00007ff8119413d7 -[NSWindow _setUpFirstResponderBeforeBecomingVisible] + 63
47 AppKit 0x00007ff8119408b4 -[NSWindow _doWindowWillBeVisibleAsSheet:] + 153
48 AppKit 0x00007ff812160752 -[NSWindow _reallyDoOrderWindowAboveOrBelow:] + 1338
49 AppKit 0x00007ff8121613b4 -[NSWindow _reallyDoOrderWindow:] + 99
50 AppKit 0x00007ff81216162a -[NSWindow _doOrderWindow:] + 295
51 AppKit 0x00007ff811933fc2 -[NSWindow makeKeyAndOrderFront:] + 47
52 QuickLookUI 0x00007ffa2c17f5ba -[QLSeamlessDocumentOpener showWindow:contentFrame:withBlock:] + 169
53 AppKit 0x00007ff811a36c1f -[NSWindowController showWindow:] + 672
54 AppKit 0x00007ff8117d884b NSApplicationMain + 997
55 CuTools 0x000000010959d0af main + 47
56 dyld 0x00007ff80e2b6310 start + 2432
)
解决方法
1、创建一个NSCollectionViewItem需要XIB
2、继承一个带有XIB的NSCollectionViewItem父类,并且实现
- (instancetype)initWithNibName:(NSNibName)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{
CuMainTabItem *item = [super initWithNibName:(NSNibName)@"父类名" bundle:nibNameOrNil];
return item;
}