Das*_*erd 6 macos cocoa nscollectionview nsscrollview swift
我无法纵向和横向滚动以使用NSCollectionView的自定义布局.根据文档,在我的子类中,我返回`collectionViewContentSize',如果它太大,则在集合视图的封闭滚动视图中自动启用滚动.但是,即使我在水平行中排序所有元素,也只启用垂直滚动.
这是一个截图:http: //i.stack.imgur.com/tMbCN.png
这是我的布局代码:
import Cocoa
class Layout: NSCollectionViewLayout {
var cellSize = CGSize(width: 100, height: 30)
var cellSpacing: CGFloat = 10
var sectionSpacing: CGFloat = 20
private var contentSize = CGSize.zero
private var layoutAttributes = [NSIndexPath: NSCollectionViewLayoutAttributes]()
override func prepareLayout() {
guard let collectionView = collectionView else { return }
let sections = collectionView.numberOfSections
guard sections > 0 else { return }
contentSize.height = cellSize.height
for section in 0..<sections {
let items = collectionView.numberOfItemsInSection(section)
guard items > 0 else { break }
for item in 0..<items {
let origin = CGPoint(x: contentSize.width, y: 0)
let indexPath = NSIndexPath(forItem: item, inSection: section)
let attributes = NSCollectionViewLayoutAttributes(forItemWithIndexPath: indexPath)
attributes.frame = CGRect(origin: origin, size: cellSize)
layoutAttributes[indexPath] = attributes
contentSize.width += cellSize.width + cellSpacing
}
contentSize.width += sectionSpacing
}
}
override var collectionViewContentSize: NSSize {
return contentSize
}
override func layoutAttributesForElementsInRect(rect: NSRect) -> [NSCollectionViewLayoutAttributes] {
return layoutAttributes.values.filter { $0.frame.intersects(rect) }
}
override func layoutAttributesForItemAtIndexPath(indexPath: NSIndexPath) -> NSCollectionViewLayoutAttributes? {
return layoutAttributes[indexPath]
}
override func shouldInvalidateLayoutForBoundsChange(newBounds: NSRect) -> Bool {
return false
}
}
Run Code Online (Sandbox Code Playgroud)
这个bug仍在继续.我把Bo Yuan讨厌的黑客改成了Swift.(没有冒犯,博远,这不是你的错,我们必须做这个可怕的解决办法!我仍然会在有正式修复的情况下尽快删除这些代码.暂时这只是为了让我的开发工作继续下去.)
class HackedCollectionView: NSCollectionView {
override func setFrameSize(_ newSize: NSSize) {
let size = collectionViewLayout?.collectionViewContentSize ?? newSize
super.setFrameSize(size)
if let scrollView = enclosingScrollView {
scrollView.hasHorizontalScroller = size.width > scrollView.frame.width
}
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,这肯定需要尽快,因为在滚动时会为每一帧动画调用它.不太好.请,请有人解决此问题或找到合适的解决方案.不能水平滚动是荒谬的.
小智 1
这基本上是 NSCollectionView 中的一个错误。作为解决方法,您可以实现scrollDirection方法(来自NSCollectionViewFlowLayout)并返回NSCollectionViewScrollDirectionHorizontal。
| 归档时间: |
|
| 查看次数: |
2463 次 |
| 最近记录: |