实际上我想提请大背景的一个选择 NSStatusItemCALayer 我的定义statusItemView.但是,因为

- (void)drawStatusBarBackgroundInRect:(NSRect)rect withHighlight:(BOOL)highlight

不工作(?) 上层我已经试过这画的颜色,与backgroundColor财产。但是转换的selectedMenuItemColor成RGB不会帮助很大。它看起来真的很普通没有梯度。:-/

我换的 [NSColor selectedMenuItemColor] 进入一个 CGColorRef 这个代号:

- (CGColorRef)highlightColor {
    static CGColorRef highlight = NULL;
    if(highlight == NULL) {
        CGFloat red, green, blue, alpha;
        NSColor *hlclr = [[NSColor selectedMenuItemColor] colorUsingColorSpace:
                     [NSColorSpace genericRGBColorSpace]];
        [hlclr getRed:&red green:&green blue:&blue alpha:&alpha];
        CGFloat values[4] = {red, green, blue, alpha};
        highlight = CGColorCreate([self genericRGBSpace], values);
    }
    return highlight;
}

任何想法如何绘制地寻找statusitem背景CALayer?

有帮助吗?

解决方案

试试继承CALayer和执行 drawInContext: 方法创建一个NSGraphicsContext的CGContext, 设置NSGraphicsContext作为目前上下文, ,然后告诉地位的项目绘制其背景。

其他提示

NSImage *backgroundImage = [[NSImage alloc] initWithSize:self.frame.size]];
[backgroundImage lockFocus];
[self.statusItem drawStatusBarBackgroundInRect:self.bounds withHighlight:YES];
[backgroundImage unlockFocus];
[self.layer setContents:backgroundImage];
[backgroundImage release];

我使用此代码在我的层代表:

- (void)drawLayer:(CALayer *)layer 
        inContext:(CGContextRef)context {
    NSGraphicsContext* gc = [NSGraphicsContext graphicsContextWithGraphicsPort:context flipped:NO];
    [NSGraphicsContext saveGraphicsState];
    [NSGraphicsContext setCurrentContext:gc];
    [self.statusItem drawStatusBarBackgroundInRect:self.frame 
                                     withHighlight:self.isHighlighted];
    [NSGraphicsContext restoreGraphicsState];
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top