gpt4 book ai didi

objective-c - NSScrollView滚动不滚动?

转载 作者:行者123 更新时间:2023-12-03 17:01:53 25 4
gpt4 key购买 nike

将显示 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.

换句话说: ScrollView 显示其文档 View 的一部分。如果文档 View 与 ScrollView 的大小相同,则它不会滚动。使文档 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]];
}

关于objective-c - NSScrollView滚动不滚动?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35664420/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com