NSScrollView滚动不起作用?

时间:2016-02-27 01:01:34

标签: objective-c macos cocoa nsscrollview

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个按钮来帮助它们。

1 个答案:

答案 0 :(得分:2)

  

滚动视图显示视图内容的一部分,该内容太大而无法在窗口中显示,并允许用户在滚动视图中移动文档视图。

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

编辑:

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