NSCollectionView不会滚动超过初始可见矩形的项目 [英] NSCollectionView does not scroll items past initial visible rect

查看:268
本文介绍了NSCollectionView不会滚动超过初始可见矩形的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试修复一些突出的macOS 10.13错误时,我遇到了一个现有应用程序的问题.我有一个小的NSCollectionView,看起来类似于Calendar应用程序中的每月小日历.它显示日期并在单击时垂直滚动.但是,在macOS 10.13上,收集视图仅显示几行,并且不会滚动.我已经验证了数据源已正确调用,并且确实尝试加载其他项-但不会滚动到它们.

I'm running into an issue with an existing app while trying to fix a few outstanding macOS 10.13 bugs. I have a small NSCollectionView that looks similar to the small monthly calendar in the Calendar app. It displays days and scrolls vertically on click. However, on macOS 10.13 the collection view only displays a few rows and does not scroll. I've verified that the data source is being called correctly and it does try to load additional items - but doesn't scroll to them.

我创建了一个小的示例应用程序,该应用程序也演示了该问题.这是一个基本的macOS应用程序,它通过Main故事板添加了NSCollectionView,并具有从笔尖加载的通用NSCollectionViewItem类.主视图控制器中的整个代码为:

I've created a small sample application that also demonstrates the issue. This is a basic macOS app that adds an NSCollectionView via the Main storyboard and has a generic NSCollectionViewItem class loaded from a nib. The entirety of the code in the main view controller is:

- (void)viewDidLoad {  
  [super viewDidLoad];  

  NSNib *nib = [[NSNib alloc] initWithNibNamed:@"FooCollectionViewItem" bundle:nil];  
  [self.collectionView registerNib:nib forItemWithIdentifier:@"foo"];  
}  
- (void)viewDidAppear {  
  [super viewDidAppear];  

  [self.collectionView reloadData];  
}  
- (void)setRepresentedObject:(id)representedObject {  
  [super setRepresentedObject:representedObject];  

}  
- (NSInteger)numberOfSectionsInCollectionView:(NSCollectionView *)collectionView {  
  return 1;  
}  
- (NSInteger)collectionView:(NSCollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {  
  return 2000;  
}  
- (NSCollectionViewItem *)collectionView:(NSCollectionView *)collectionView itemForRepresentedObjectAtIndexPath:(NSIndexPath *)indexPath {  
  FooCollectionViewItem *item = [collectionView makeItemWithIdentifier:@"foo" forIndexPath:indexPath];  
  item.fooField.stringValue = [NSString stringWithFormat:@"%ld", indexPath.item];  

  return item;  
}

生成的应用程序如下:

不幸的是,这完全是集合视图的全部.滚动不会滚动任何其他项目.我已将示例应用程序上传到 https://www.dropbox.com/sh /wp2y7g0suemzcs1/AABVKfTZq54J7riy6BR7Mhxha?dl = 0 (如果有用).有什么想法为什么会在我的10.13上打破吗?

Unfortunately, that's the entirely of the collection view. Scrolling doesn't scroll through any additional items. I've uploaded the sample app to https://www.dropbox.com/sh/wp2y7g0suemzcs1/AABVKfTZq54J7riy6BR7Mhxha?dl=0 in case that's useful. Any ideas why this is breaking on 10.13 for me?

推荐答案

我在运行High Sierra的NSCollectionView滚动中遇到了类似的问题.这是我解决的方法:

I had a similar issue with NSCollectionView scroll running High Sierra. Here's how I solved it:

SWIFT

  if #available(OSX 10.13, *) {
    if let contentSize = self.collectionView.collectionViewLayout?.collectionViewContentSize {
      self.collectionView.setFrameSize(contentSize)
    }
  }

OBJ-C

  if (@available(macOS 10.13, *)) {
    [self.collectionView setFrameSize: self.collectionView.collectionViewLayout.collectionViewContentSize];
  }

这篇关于NSCollectionView不会滚动超过初始可见矩形的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆