0

在我基于文档的 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];
4

2 回答 2

0

我还没有完全解决这个问题,但我想出了一个我认为我会分享的 hacky 解决方案。现在,我正在调用一个单独的方法来在启动后立即调整窗口大小。这是在 windowControllerDidLoadNib 的末尾:

[self performSelector: @selector(resizeWidth) withObject: nil afterDelay:0.0f];

这调用:

-(void)resizeWidth
{
NSRect newFrame = NSMakeRect(oldFrame.origin.x, oldFrame.origin.y, 975, oldFrame.size.height);
[[self docWindow]setFrame:newFrame display:YES animate:YES];
}

我不喜欢这个解决方案,特别是因为它创建了一个非常小的动画来吸引用户的眼球,但它非常快,通常不引人注意,并且总是比原始问题更不具有侵入性。至少这样,用户可以立即使用应用程序的滚动条,并且 UI 功能无需用户调整窗口大小。

于 2013-11-06T18:14:12.050 回答
0

我运行了您的代码(稍作修改以查看滚动条)。你在 Interface Builder 的 NSScrollView 上设置了什么属性?另外,您在哪里设置文档视图等?(您的上述代码)。我在 NSDocument 的以下方法中将它们全部初始化,一切正常!

- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
    [super windowControllerDidLoadNib:aController];

    float allHeight = 0.f; // Consider starting from the top
    float spacing = 4.f; // Spacing
    float buttonCellHeight = 4000.f;

    CustomView *navView = [[CustomView alloc]init];
    //1
    NSButton *overviewButton = [[NSButton alloc]initWithFrame:NSMakeRect(0, allHeight, _scrollView.bounds.size.width, buttonCellHeight)];

    // Setup overviewButton properties
    [navView addSubview:overviewButton];
    allHeight += buttonCellHeight;
    allHeight += spacing;

    //2
    NSButton *curriculumButton = [[NSButton alloc]initWithFrame:NSMakeRect(0, allHeight, _scrollView.bounds.size.width, buttonCellHeight)];

    // setup curriculumButton properties
    [navView addSubview:curriculumButton];
    allHeight += buttonCellHeight;
    allHeight += spacing;

    [navView setFrame:NSMakeRect(1, 1, _scrollView.bounds.size.width, allHeight)];
    [_scrollView setDocumentView:navView];
}
于 2013-11-01T20:43:30.973 回答