有谁知道将NSScrollView滚动到顶部的正确方法?我正在寻找与UIView的scrollToTop方法等效的方法。
这是我到目前为止所做的,但在所有情况下都不太正确
[self.contentView scrollToPoint:NSMakePoint(0, MAX(contentView.frameHeight, ((NSView*)self.documentView).frameHeight))];
答案 0 :(得分:8)
终于明白了。像魅力一样工作!
if ([self hasVerticalScroller]) {
self.verticalScroller.floatValue = 0;
}
NSPoint newOrigin = NSMakePoint(0, NSMaxY(((NSView*)self.documentView).frame) - self.boundsHeight);
[self.contentView scrollToPoint:newOrigin];
答案 1 :(得分:5)
针对 Swift 4 进行了更新:
如果您的文档视图被翻转:
scrollView.documentView?.scroll(.zero)
否则:
if let documentView = scrollView.documentView {
documentView.scroll(NSPoint(x: 0, y: documentView.bounds.size.height))
}
答案 2 :(得分:4)
这也很好。设置NSScrollView的documentView的滚动点
NSPoint pt = NSMakePoint(0.0, [[self.scrollView documentView]
bounds].size.height);
[self.scrollView.documentView scrollPoint:pt];
答案 3 :(得分:1)
以前的解决方案在转换为适用于Mac OS的Swift时会滚动到底部。 y坐标方向在Mac OS和iOS上翻转。
适用于Mac OS的Swift 3解决方案:
scrollView.documentView?.scroll(NSPoint.zero)