2013-10-29 57 views
0

在我的基於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]; 
+0

我認爲你應該讓你的嵌入式視圖成爲它想要的大小(不要將其大小設置爲scollview的大小)。其餘的滾動視圖會擔心。 – trojanfoe

+0

我把它切換出來,給嵌入式視圖設置一個寬度,但我仍然得到相同的行爲。 –

回答

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

我已經嘗試翻轉每個可能的IB設置,一次一個,無濟於事。 –

+0

現在只有兩個框被選中:顯示垂直滾動條和自動調整子視圖。我還玩過自動隱藏並添加了水平滾動條。添加水平滾動器導致兩個滾動器的行爲類似。沒有邊界,水平和垂直彈性設置爲自動。我沒有對IB中的滾動器進行其他設置 - 所有默認位置仍然存在。我在windowControllerDidLoadNib的NSDocument子類中調用了上述代碼,或者在名爲there的單獨方法中調用了上述代碼,結果相同。 –

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的功能無需用戶調整窗口大小。

+0

您的項目的任何zip可用嗎? –

+0

我正在提交一個版本,然後我會發佈一個!感謝您的關注! –

相關問題