NSScrollView scrollToTop

2019-05-03 06:22发布

Does anyone know the correct way to scroll the NSScrollView to the top? I was looking for an equivalent to the UIView's scrollToTop method.

This is what I have so far, but its not quite right under all situations

[self.contentView scrollToPoint:NSMakePoint(0, MAX(contentView.frameHeight, ((NSView*)self.documentView).frameHeight))];

4条回答
够拽才男人
2楼-- · 2019-05-03 06:26

This also works fine. Set scroll point of documentView of NSScrollView

NSPoint pt = NSMakePoint(0.0, [[self.scrollView documentView]
                               bounds].size.height);
[self.scrollView.documentView scrollPoint:pt];
查看更多
ゆ 、 Hurt°
3楼-- · 2019-05-03 06:26

The previous solutions scroll to the bottom when translating to Swift for Mac OS. The y coordinate direction is flipped on Mac OS and iOS.

Swift 3 Solution for Mac OS:

scrollView.documentView?.scroll(NSPoint.zero)
查看更多
我想做一个坏孩纸
4楼-- · 2019-05-03 06:31

Updated for Swift 4:

If your document view is flipped:

scrollView.documentView?.scroll(.zero)

otherwise:

if let documentView = scrollView.documentView {
        documentView.scroll(NSPoint(x: 0, y: documentView.bounds.size.height))
}
查看更多
霸刀☆藐视天下
5楼-- · 2019-05-03 06:35

Finally figured it out. Works like a charm!

if ([self hasVerticalScroller]) {
    self.verticalScroller.floatValue = 0;
}

NSPoint newOrigin = NSMakePoint(0, NSMaxY(((NSView*)self.documentView).frame) - self.boundsHeight);
[self.contentView scrollToPoint:newOrigin];
查看更多
登录 后发表回答