Scroll NSScrollView(Mac) to Top

Aijin Yuan (Vince)
1 min readDec 27, 2011

--

AppKit is old and is not as convenient as UIKit. But we have to use it on Mac app development.

“Scroll a scrollview to top” sounds easy, but actually it isn’t. If you assign a documentView to a NSScrollView, you will see it scrolls to the bottom. (Very stupid!) The doc did not mention how to scroll to top. I found a way to control the vertical scroller. I can set the scroller to top, but the scrollView is still at bottom!

This post (http://stackoverflow.com/questions/4506391/nsscrollview-jumping-to-bottom-on-scroll) inspired me. I got the solution (of cause, I post my answer on stackoverflow too):

    // Scroll the vertical scroller to top
if ([_scrollView hasVerticalScroller]) {
_scrollView.verticalScroller.floatValue = 0;
}
// Scroll the contentView to top
[_scrollView.contentView scrollToPoint:NSMakePoint(0, ((NSView*)_scrollView.documentView).frame.size.height - _scrollView.contentSize.height)];

Originally published at vinceyuan.github.io on December 27, 2011.

--

--

Aijin Yuan (Vince)

iOS/Android/Web developer. UX engineering lead at Grab. Singapore/Hong Kong/Shanghai