2014-11-14 28 views
8

我試圖添加一個按鈕欄到我的基於NSOutlineView的源列表的底部,如許多Mac應用程序(Apple和第三方三方),如在這些截圖:將控件/按鈕欄添加到配置爲源列表的NSOutlineView的底部

Xcode Source List Footer Mail Source List Footer

爲了以文字描述它,控制條股源列表的特殊風格的梯度背景(或約塞米蒂下,「活力」)不重疊任何的源列表的內容。在試圖複製這種效果,我曾嘗試以下方法至今:

  1. 添加額外的「填充」與按鈕欄到源列表的滾動視圖的夾視圖底部的高度,並允許酒吧重疊源列表視圖。這隻適用於按鈕欄背景不透明且不移動滾動條的情況。
  2. 使用NSBox es,其背景設置爲由源列表的backgroundColor屬性提供的NSColor。這需要大量強制重繪才能正確繪製(特別是在窗口活動/非活動狀態),但看起來很完美。用自定義的NSView設置繪製漸變背景可以看到類似的行爲。

是否有任何其他方法可用於實現此目的? #2是我能夠到達的最接近的,但考慮到它帶來的問題,它顯然不適合第三方開發人員使用。

在優勝美地與Vibrancy一起做這件事應該很簡單,就是檢查優勝美地並插入一個打開活力的NSVisualEffectView。從另一方面來看,它正好在10.8/10.9以下......

我可以完全避開這個問題,通過使用NSWindow提供的內置底部條形圖,但顏色合併的方法在視覺上更清潔,更強烈將控制與他們的父窗格聯繫起來,而且這些日子似乎越來越多地成爲選擇的風格。如果可能,我想在我自己的應用程序中使用它。

回答

1

我能夠通過繼承NSView並使用KVO在包含窗口的按鍵狀態更改時觀察顏色更改來獲得此工作。由於你提到的原因(振動),它在10.10上不起作用,但它在10.9上完美工作。

蘋果郵件列表上的Sourcelist background colors線程是爲我解決它的。

接口:

#import <Cocoa/Cocoa.h> 

@interface SourceListColoredView : NSView 
@end 

實現:

#import "SourceListColoredView.h" 

@interface SourceListColoredView() 
@property (nonatomic, strong) NSColor *backgroundColor; 
@property (nonatomic, assign, getter = isObservingKeyState) BOOL observingKeyState; 
@end 

@implementation SourceListColoredView 

- (id)initWithFrame:(NSRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     [self addWindowKeyStateObservers]; 
    } 
    return self; 
} 

- (void)awakeFromNib 
{ 
    NSOutlineView *outlineView = [[NSOutlineView alloc] init]; 
    [outlineView setSelectionHighlightStyle:NSTableViewSelectionHighlightStyleSourceList]; 
    self.backgroundColor = [outlineView backgroundColor]; 
    [self addWindowKeyStateObservers]; 
} 

- (void)addWindowKeyStateObservers 
{ 
    if (!self.isObservingKeyState) { 
     [[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(redisplay) 
                name:NSWindowDidBecomeKeyNotification 
                object:[self window]]; 

     [[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(redisplay) 
                name:NSWindowDidResignKeyNotification 
                object:[self window]]; 
    } 
    self.observingKeyState = YES; 
} 

- (void)redisplay 
{ 
    [self setNeedsDisplay:YES]; 
} 

- (void)dealloc 
{ 
    if (self.isObservingKeyState) { 
     [[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidBecomeKeyNotification object:[self window]]; 
     [[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidResignKeyNotification object:[self window]]; 
    } 
} 

- (void)drawRect:(NSRect)dirtyRect 
{ 
    [_backgroundColor setFill]; 
    NSRectFill(dirtyRect); 
} 

@end 

您可能需要在-awakeFromNib別的地方移動代碼取決於你如何初始化視圖。

0

我找到了解決方案: 我把一個像你這樣的NSBox放在SplitView左邊View的底部。 這裏是我的看法層次:

Hierarchical view of components in Interface Builder

的解決方法是在NSBox的檢查員填寫正確的參數:

parameters of the box

我已經在這裏設置框中鍵入自定義並填充顏色爲白色。

我希望這個答案對你有幫助。

+0

當源列表的內容足以滾動時會發生什麼?滾動條有多高?它是否重疊或低於箱子?源列表中的項目是否通過框顯示? –

+0

這些項目沒有在上面顯示,但隱藏在框中。滾動條會放在盒子上,但就是這樣。對於完整的項目,請到這裏:[link](https://github.com/zarghol/SeriesViewer) – zarghol