在我基于文档的 OS X 应用程序中,我在 xib 中有一个空的 NSScrollView,并设置了“显示垂直滚动条”。在运行时,我生成一个带有导航按钮的视图,我将其设置为 NSScrollView 的文档视图。大多数情况下,这很好用,但大约 25% 的时间,似乎是随机的(或者至少我还不能重现任何特定条件!),而不是侧面的滚动条,我得到一个黑条,其中滚动条应该是。如果我调整窗口大小,黑条消失,滚动条出现并且工作正常。
结果是,如果我一个接一个地打开 6 或 7 个空白文档,其中大约 3 个将没有立即运行的滚动视图。
这是使用导航按钮创建视图并将其添加为 NSScrollview 的 documentView 的代码。
YMScrollDocView 是 NSView 的子类,将 isFlipped 设置为 YES。navScrollView 是 xib 中滚动视图的出口。
float allHeight = 0.f; // Consider starting from the top
//float xOffset = 2.f; // Offset addjustments
float spacing = 4.f; // Spacing
float buttonCellHeight = 40.f;
YMScrollDocView *navView = [[YMScrollDocView alloc]init];
//1
NSButton *overviewButton = [[NSButton alloc]initWithFrame:NSMakeRect(0, allHeight, self.navScrollView.bounds.size.width, buttonCellHeight)];
[overviewButton setTitle:@"Overview"];
[overviewButton setButtonType:NSMomentaryLight];
[overviewButton setBordered:NO];
[[overviewButton cell]setBackgroundColor:[self colorWithHexColorString:@"30BDF8"]];
[overviewButton setBezelStyle:NSRegularSquareBezelStyle];
[overviewButton setButtonType:NSCellIsBordered];
[overviewButton setTarget:self];
[overviewButton setAction:@selector(goToOverview)];
[overviewButton setRefusesFirstResponder:YES];
[navView addSubview:overviewButton];
allHeight += buttonCellHeight;
allHeight += spacing;
//2
NSButton *curriculumButton = [[NSButton alloc]initWithFrame:NSMakeRect(0, allHeight, self.navScrollView.bounds.size.width, buttonCellHeight)];
[curriculumButton setTitle:@"Curriculum"];
[curriculumButton setButtonType:NSMomentaryLight];
[curriculumButton setBordered:NO];
[[curriculumButton cell]setBackgroundColor:[self colorWithHexColorString:@"36FBF8"]];
[curriculumButton setBezelStyle:NSRegularSquareBezelStyle];
[curriculumButton setButtonType:NSCellIsBordered];
[curriculumButton setTarget:self];
[curriculumButton setAction:@selector(goToCurriculum)];
[curriculumButton setRefusesFirstResponder:YES];
[navView addSubview:curriculumButton];
allHeight += buttonCellHeight;
allHeight += spacing;
(我在这里添加了更多带有重复代码的按钮,然后以下面的代码结束。)
[navView setFrame:NSMakeRect(1, 1, self.navScrollView.bounds.size.width, allHeight)];
[[self navScrollView]setDocumentView:navView];