views:

333

answers:

2

I've created an NSScrollView which itself contains a NSClippedView as content view (this is all default, created by IB). Inside the contents view there is the (default) document view.

This NSScrollView has horizontal scroller disabled and vertical enabled and, most importantly auto hide scrollers enabled.

When I add new views (via code, runtime) to the document view the scroller does not unhide automatically, until the moment I vertically resize the window (and which in turn resizes the scrollview as well). 1px is enough. Just the new painting of the window seems enough.

What I am looking for is triggering this by code: so when I add views to the scrollviews' content view I would like the scrollbar to appear.

int numberOfChildViews = 10; //hard coded for example here
int childViewHeight = 80; //same as above

NSRect rect = NSMakeRect(0, 0, [[self.scrollView contentView] bounds].size.width, [numberOfChildViews*childViewHeight);
[[self.scrollView documentView] setFrame:rect];
[[self.scrollView documentView] setBounds:rect]; //just added to make sure

Then I added the custom views to the document view, like:

for (int i=0; i<numberOfChildViews; i++) {
    NZBProgressViewController *item = [nzbProgressArray objectAtIndex:i];

    int y=i*[[item view] bounds].size.height;
    rect= NSMakeRect(0, y, [[scrollView contentView] frame].size.width, [[item view] bounds].size.height);

            [[item view] setFrame:rect];
    currentPosition++;
}

I am using a FlippedView so the origin will be displayed in left-top, like so:

@interface NSFlippedClipView : NSClipView {
}

@implementation NSFlippedClipView
- (BOOL)isFlipped {
    return YES;
}

- (BOOL)isOpaque {
    return YES;
}
@end

And added the following code to the awakeFromNib

NSFlippedClipView *documentView = [[NSFlippedClipView alloc] init];
[documentView setAutoresizingMask:NSViewWidthSizable];
[documentView setBackgroundColor:[self.scrollView backgroundColor]];

[self.scrollView setDocumentView:documentView];
[documentView release];
A: 

The scrollbars should become visible as soon as the document view is resized to be larger than the current viewport of the scroll view. Are you resizing the document view when you add your subviews to it?

Rob Keniger
Thanks for answering. I will post the code this evening. What I do is:during awakefromnib is set the document view of the scrollview to a custom overload of nsclipview, only overloaded method is IsFlipped, this will result YES to make the origin left-top. This view I called NSFlippedClipViewDuring adding of dynamic content to the document view I resize the document view to: vertical size of the contenview (which is the 'viewport' on the documentview) and the heigh to the number of custom views * the height of the custom views. Which all seem to work fine.
Ger Teunis
A: 

Ahh, it's my own bad. For future reference: if you want to move the origin (0,0) to left-top use a NSView instead of NSClippedView extended class with IsFlipped method overriden to YES.

Thanks irsk for answering.

Ger Teunis