user2403221
user2403221

Reputation: 445

Horizontal NSCollectionView in vertically scrolling NSTableView Swift macOS

I created a vertically scrolling NSTableView. Each cell of this table view contains a title and beneath is a horizontally scrolling NSCollectionView. When the cursor is on the title when scrolling, the vertical tableview scrolls fine, but when the cursor is on the horizontally scrolling collection view the table view doesn't scroll.

How can I get the table view to scroll even when the cursor is on the collection view? I understand that the tableview and scrollview use two separate scroll views, but I don't know if there is a way to synchronize these two.

Upvotes: 1

Views: 655

Answers (1)

user2403221
user2403221

Reputation: 445

Ok thank god, I found a solution here on stackoverflow after all. It was in Objective-C so I translated it to Swift. Here it is for anybody looking at a similar problem:

class MTHorizontalScrollView: NSScrollView {

var currentScrollIsHorizontal = false

override func draw(_ dirtyRect: NSRect) {
    super.draw(dirtyRect)
}

override func scrollWheel(with event: NSEvent) {
    if event.phase == NSEvent.Phase.began || (event.phase == NSEvent.Phase.ended && event.momentumPhase == NSEvent.Phase.ended) {
        currentScrollIsHorizontal = fabs(event.scrollingDeltaX) > fabs(event.scrollingDeltaY)
    }
    if currentScrollIsHorizontal {
        super.scrollWheel(with: event)
    } else {
        self.nextResponder?.scrollWheel(with: event)
    }
}

}

Full credit to SO LINK

Upvotes: 1

Related Questions