1
votes

Eight of the 20 buttons are to show. But I can't scroll through the rest? Is a scroll bar supposed to show?

NSScrollView *nssvFonts = [[NSScrollView alloc] initWithFrame:CGRectMake(200, 200, 200, 400)];
[self.view addSubview:nssvFonts];

[nssvFonts.documentView setFrame: NSMakeRect(0,0,200, 400) ];
nssvFonts.hasVerticalScroller = YES;

for(int i = 0; i < 20; i++){
    NSButton *btnDown = [[NSButton alloc] initWithFrame:CGRectMake(0, 50 * i, 200, 50)];
    [nssvFonts addSubview:btnDown];
    [btnDown setButtonType:NSMomentaryPushInButton];
    [btnDown setTitle:[NSString stringWithFormat:@"Down: %d", i]];
}

So I applied Willeke's advice and made the addition and changes:

NSScrollView *nssvFonts = [[NSScrollView alloc] initWithFrame:CGRectMake(200, 200, 300, 400)];
[self.view addSubview:nssvFonts];

[nssvFonts.documentView setFrame: NSMakeRect(0,0,200, 1000)];
//[nssvFonts.contentView setFrame:NSMakeRect(0,  0, 200, 400)];
nssvFonts.hasVerticalScroller = YES;

for(int i = 0; i < 20; i++){
    NSButton *btnDown = [[NSButton alloc] initWithFrame:CGRectMake(50, 50 * i, 200, 50)];
    [nssvFonts addSubview:btnDown];
    [btnDown setButtonType:NSMomentaryPushInButton];
    [btnDown setTitle:[NSString stringWithFormat:@"Down: %d", i]];
}

But still no vertical scroll bar is showing and I can't use the mouse to scroll through it either... 1 button down or 2 buttons down to help either.

1

1 Answers

2
votes

A scroll view displays a portion of the contents of a view that’s too large to be displayed in a window and allows the user to move the document view within the scroll view.

In other words: a scroll view displays a portion of its document view. If the document view is the same size as the scroll view, it won't scroll. Make the document view big enough so that all buttons will fit in.

Edit:

NSScrollView *nssvFonts = [[NSScrollView alloc] initWithFrame:NSMakeRect(200, 200, 300, 400)];
[self.view addSubview:nssvFonts];

NSView *documentView = [[NSScrollView alloc] initWithFrame:NSMakeRect(0, 0, 200, 1000)];
nssvFonts.documentView = documentView;

nssvFonts.hasVerticalScroller = YES;

for(int i = 0; i < 20; i++) {
    NSButton *btnDown = [[NSButton alloc] initWithFrame:NSMakeRect(50, 50 * i, 200, 50)];
    [documentView addSubview:btnDown];
    [btnDown setButtonType:NSMomentaryPushInButton];
    [btnDown setTitle:[NSString stringWithFormat:@"Down: %d", i]];
}