NSScrollView 滚动不滚动?

NSScrollView scroll is not scrolling?

要显示 20 个按钮中的 8 个。但我无法滚动浏览其余部分?应该显示滚动条吗?

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]];
}

所以我采纳了 Willeke 的建议并进行了添加和更改:

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]];
}

但仍然没有显示垂直滚动条,我也无法使用鼠标滚动它...按下 1 个按钮或按下 2 个按钮都可以提供帮助。

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.

换句话说:滚动视图显示其文档视图的一部分。如果文档视图与滚动视图的大小相同,则它不会滚动。使文档视图足够大,以便所有按钮都能容纳。

编辑:

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]];
}