I have a trouble with a NSScrollview in my aplication because it always start at the bottom of the window. How could make it start in the top?
Asked
Active
Viewed 5,915 times
13
-
You like to scroll NSScrollView programmatically? – Anne Apr 29 '11 at 18:55
3 Answers
13
Try something like this:
NSPoint pointToScrollTo = NSMakePoint ( , ); // Any point you like.
[[scrollView contentView] scrollToPoint: pointToScrollTo];
[scrollView reflectScrolledClipView: [scrollView contentView]];

Anne
- 26,765
- 9
- 65
- 71
-
This works well in swift too, adapted slightly: scrollView!.documentView = toolsView var scrollLocation = NSPoint(x: 0, y: toolsView.frame.size.height - leftScrollView!.contentView.frame.size.height) leftScrollView!.contentView.scrollToPoint(scrollLocation) leftScrollView!.reflectScrolledClipView(leftScrollView!.contentView) – Jay Mar 05 '15 at 03:12
4
A solution is given in the Scroll View Programming Guide, Scrolling to a Specific Location. You can use -[NSView scrollPoint:]
to set the clip view's origin.
// A controller which has an outlet to the scroll view
- (void)awakeFromNib {
// Not checking for flipped document view. See sample code.
// New origin should be
// (documentView.frame.y + documentView.frame.height) - clipView.bounds.height
NSPoint newOrigin = NSMakePoint(0, NSMaxY([[scrollView documentView] frame]) -
[[scrollView contentView] bounds].size.height);
[[scrollView documentView] scrollPoint:newOrigin];
}

jscs
- 63,694
- 13
- 151
- 195
-
It works for me but : `NSPoint newOrigin = NSMakePoint(0, NSMaxY([[scrollView documentView] frame]) - [[scrollView contentView] bounds].size.height); [[scrollView documentView] scrollPoint:newOrigin];`` – Colas Mar 26 '13 at 18:09
3
If you have a custom NSView subclass inside the NSScrollView, try overriding isFlipped
:
- (BOOL)isFlipped
{
return YES;
}
This puts the view's origin at the top, which should make the NSScrollView do what you want.
However, it will also flip the coordinates of everything inside your view.

Selcaby
- 71
- 5