我以编程方式在NSCollectionView中选择一个项目。该项目按预期选择,但视图不会自动滚动到此项目。
[collectionView setSelectionIndexes:[NSIndexSet indexSetWithIndex:compt]];
如何滚动到所选项目?
答案 0 :(得分:6)
在此处找到答案NSScrollView: Make sure frame is visible
此代码对我有用:
NSRect selectionRect = [self.collectionView frameForItemAtIndex:[[self.collectionView selectionIndexes] firstIndex]];
[self.collectionView scrollRectToVisible:selectionRect];
答案 1 :(得分:3)
由于新的NSCollectionView中的OS X 10.11,您应该使用:
[self.collectionView scrollToItemsAtIndexPaths:[self.collectionView selectionIndexPaths] scrollPosition:NSCollectionViewScrollPositionCenteredHorizontally];
答案 2 :(得分:0)
让scrollPositionX:CGFloat = videoThumbCollectionView.visibleRect.origin.x + 150.0 videoThumbCollectionView.scroll(NSPoint(x:scrollPositionX,y:videoThumbCollectionView.visibleRect.origin.y))
答案 3 :(得分:0)
对于Swift 4
collectionView.scrollToItems(at: [collectionView.selectionIndexPaths.first!], scrollPosition: NSCollectionView.ScrollPosition.centeredHorizontally)
答案 4 :(得分:0)
经过以下测试:Swift 4.2。 Xcode 10.2。 macOS 10.14.5 Beta
if let indexPath = indexPaths.first {
if #available(OSX 10.12, *) {
NSAnimationContext.runAnimationGroup { (context) in
context.duration = 0.8
context.allowsImplicitAnimation = true
collectionView.scrollToItems(at: [indexPath], scrollPosition: NSCollectionView.ScrollPosition.bottom)
}
} else {
collectionView.scrollToItems(at: [indexPath], scrollPosition: NSCollectionView.ScrollPosition.bottom)
}
}