在我的基於OS X應用程序的文檔中,我在xib中有一個空的NSScrollView,並設置了「顯示垂直滾動條」。在運行時,我用導航按鈕生成一個視圖,將其設置爲NSScrollView的文檔視圖。大多數情況下,這種方式效果不錯,但大約有25%的時間,似乎是隨機的(或者至少我不能再現任何特定的情況!),而不是側面的滾動條,我會在黑色的酒吧滾輪應該是。如果我調整窗口大小,黑色條消失,滾動條出現並且工作正常。NSScrollView有時有一個黑色條而不是滾動條
結果是,如果我一個接一個地打開六個或七個空白文檔,其中大約三個不會立即運行滾動視圖。
下面是使用導航按鈕創建視圖並將其添加爲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];
我認爲你應該讓你的嵌入式視圖成爲它想要的大小(不要將其大小設置爲scollview的大小)。其餘的滾動視圖會擔心。 – trojanfoe
我把它切換出來,給嵌入式視圖設置一個寬度,但我仍然得到相同的行爲。 –