haseo98
haseo98

Reputation: 837

How to use NSScrollview?

I can't figure out how to actually use NSScrollview. I dragged the scroll view object onto an NSWindow in the interface builder. I then dragged some NSButtons onto the scroll view. My question is:

How do I actually make it scroll down, for example, 2x the original height?

Upvotes: 3

Views: 5526

Answers (1)

torrey.lyons
torrey.lyons

Reputation: 5589

Of course the user can scroll automatically using their UI. I assume what you want to do is to scroll programmatically.

A bit of background: An NSScrollView has a documentView, which is the underlying view that the scroll view shows a part of, and a clipView, which is the view that is shown on the screen. So the clip view is like a window into the document view. To scroll programmatically you tell the document view to scroll itself in the clip view.

You have two options on how to scroll programmatically:

  • - (void)scrollPoint:(NSPoint)aPoint –– This scrolls the document so the given point is at the origin of the clip view that encloses it.
  • - (BOOL)scrollRectToVisible:(NSRect)aRect –– This scrolls the document the minimum distance so the entire rectangle is visible. Note: This may not need to scroll at all in which case it returns NO.

So, for example, here is an example from Apple's Scroll View Programming Guide on how to scroll to the bottom of the document view. Assuming you have an IBOutlet called scrollView connected up to the NSScrollView in your nib file you can do the following:

- (void)scrollToBottom
{
    NSPoint newScrollOrigin;

    if ([[scrollview documentView] isFlipped]) {
        newScrollOrigin = NSMakePoint(0.0,NSMaxY([[scrollview documentView] frame])
                                       -NSHeight([[scrollview contentView] bounds]));
    } else {
        newScrollOrigin = NSMakePoint(0.0,0.0);
    }
    [[scrollview documentView] scrollPoint:newScrollOrigin];
}

Upvotes: 6

Related Questions