Ich kann nicht vertikal und horizontal scrollen, um mit einem benutzerdefinierten Layout für NSCollectionView zu arbeiten. Gemäß den Dokumenten, gebe ich in meiner Unterklasse die `collectionViewContentSize 'zurück und wenn diese zu groß ist, wird das Scrollen automatisch in der umschließenden Bildlaufansicht der Sammlungsansicht aktiviert. Auch wenn ich alle Elemente in einer horizontalen Reihe anordne, ist nur das vertikale Scrollen aktiviert. HierNSCollectionView benutzerdefinierte Layout aktivieren Bildlauf
ist ein Screenshot: http://i.stack.imgur.com/tMbCN.png
Hier ist mein Layout-Code:
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
}
}
In 'Überschreibung var collectionViewContentSize', können Sie' contentSize' und 'self.collectionView.frame' (oder assimiliert) drucken? – Larme
contentSize: (22200.0, 30.0) frame: (0.0, 0.0, 438.0, 228.0) – DasNilpferd
Und es funktioniert vertikal, wenn ich mein Layout in eine vertikale Spalte ändere, kann ich bis zum letzten Element blättern. Nur horizontales Scrollen ist immer deaktiviert. – DasNilpferd