如何在OSX中更改NSWindow标题栏的颜色

21

在长时间寻找有关 NSWindow 标题栏颜色和标题颜色的信息后,我找到了一种简单的绘图解决方案。我发布这篇文章是为了分享我的知识。


如果有其他人误入此处并想了解NSWindow是什么,请参阅NSWindow文档 - Ed Randall
3个回答

27

有一个更简单的方法来做到这一点。我已经阅读过可以在Storyboard / .xib文件中将窗口外观更改为“纹理”的方法,但那并不能完全生效(标题栏颜色是斑驳的)。然而,你只需要使用两行代码就可以在代码中更改颜色:

window.titlebarAppearsTransparent = true // gives it "flat" look
window.backgroundColor = <NSColor> // set the background color

@Tommy 没问题。 - Sam Claus
这是最简单和最优雅的方式。谢谢。 - rmvz3
1
这种方法有一个缺陷:当您想从该窗口呈现一个模态表单时,该表单将从窗口的顶部边缘下降,而不是从标题栏下方。 - Lvsti
这很棒,但如果我将其设置为深色,窗口标题仍然是黑色的,非常难以阅读。有没有办法设置窗口标题的颜色? - user1118321
这种方法的另一个缺陷是,编辑工具栏的模态表单将直接打开在要编辑的工具栏正上方。 - deflozorngott
显示剩余4条评论

22

子类化一个名为MyTitleViewNSView,并添加以下代码

- (void)drawString:(NSString *)string inRect:(NSRect)rect {
    static NSDictionary *att = nil;
    if (!att) {
        NSMutableParagraphStyle *style = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
        [style setLineBreakMode:NSLineBreakByTruncatingTail];
        [style setAlignment:NSCenterTextAlignment];
        att = [[NSDictionary alloc] initWithObjectsAndKeys: style, NSParagraphStyleAttributeName,[NSColor whiteColor], NSForegroundColorAttributeName,[NSFont fontWithName:@"Helvetica" size:12], NSFontAttributeName, nil];
        [style release];

    }

    NSRect titlebarRect = NSMakeRect(rect.origin.x+20, rect.origin.y-4, rect.size.width, rect.size.height);


    [string drawInRect:titlebarRect withAttributes:att];
}


- (void)drawRect:(NSRect)dirtyRect
{
    NSRect windowFrame = [NSWindow  frameRectForContentRect:[[[self window] contentView] bounds] styleMask:[[self window] styleMask]];
    NSRect contentBounds = [[[self window] contentView] bounds];

    NSRect titlebarRect = NSMakeRect(0, 0, self.bounds.size.width, windowFrame.size.height - contentBounds.size.height);
    titlebarRect.origin.y = self.bounds.size.height - titlebarRect.size.height;

    NSRect topHalf, bottomHalf;
    NSDivideRect(titlebarRect, &topHalf, &bottomHalf, floor(titlebarRect.size.height / 2.0), NSMaxYEdge);

    NSBezierPath * path = [NSBezierPath bezierPathWithRoundedRect:self.bounds xRadius:4.0 yRadius:4.0];
    [[NSBezierPath bezierPathWithRect:titlebarRect] addClip];



    NSGradient * gradient1 = [[[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:0.0 alpha:1.0] endingColor:[NSColor colorWithCalibratedWhite:1 alpha:1.0]] autorelease];
    NSGradient * gradient2 = [[[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:1 alpha:1.0] endingColor:[NSColor colorWithCalibratedWhite:0 alpha:1.0]] autorelease];

  [path addClip];


//    [[NSColor colorWithCalibratedWhite:0.00 alpha:1.0] set];
//   [path fill];


   [gradient1 drawInRect:topHalf angle:270.0];
    [gradient2 drawInRect:bottomHalf angle:270.0];

    [[NSColor blackColor] set];
    NSRectFill(NSMakeRect(0, -4, self.bounds.size.width, 1.0));


    [self drawString:@"My Title" inRect:titlebarRect];


}

在 appDelegate 中导入 MyTitleView 类,并添加以下代码

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
    NSRect boundsRect = [[[_window contentView] superview] bounds];
    BlackTitlebarView * titleview = [[BlackTitlebarView alloc] initWithFrame:boundsRect];
    [titleview setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];

    [[[_window contentView] superview] addSubview:titleview positioned:NSWindowBelow relativeTo:[[[[_window contentView] superview] subviews] objectAtIndex:0]];
}

11
这绝对有效,但是苹果公司在Yosemite系统中指出:“自WWDC种子版本以来新引入的功能:NSWindow从未支持客户端向除contentView之外的任何其他视图添加子视图。有些应用程序会将子视图添加到contentView.superview(也称为窗口的边框视图)。当NSWindow检测到这种情况时,它现在会记录日志:“NSWindow警告:正在添加一个未知的子视图:”。这会阻止10.10上的新功能正常工作,因此进行此操作的应用程序需要修复此问题。请参见titlebarAccessoryViewControllers以获取官方API。” - Ben Flynn

1
没有在OSX 10.10上出现警告信息,类似于这样
NSView *windowTopView = [[_window standardWindowButton:NSWindowCloseButton] superview];
-(void) applicationDidFinishLaunching:(NSNotification *)aNotification {

NSRect boundsRect = [windowTopView bounds];

NSView *windowTopView = [[_window standardWindowButton:NSWindowCloseButton] superview];
BTitleView * titleview = [[BTitleView alloc] initWithFrame:boundsRect];
[titleview setTitleImagePath:[[NSBundle mainBundle] pathForResource:@"Top1-01" ofType:@"bmp"]] ;
[titleview setAutoresizingMask:(NSViewWidthSizable | NSViewHeightSizable)];
[windowTopView addSubview:titleview positioned:NSWindowBelow relativeTo:[[windowTopView subviews] objectAtIndex:0]];

//addTitlebarAccessoryViewController
NSTitlebarAccessoryViewController *dummyTitlebarAccessoryViewController;
NSView * logoview = [[NSView alloc] initWithFrame:NSMakeRect(0, 0, 10, 11)];
dummyTitlebarAccessoryViewController = [NSTitlebarAccessoryViewController new];
dummyTitlebarAccessoryViewController.view = logoview;
dummyTitlebarAccessoryViewController.fullScreenMinHeight = 0;
[_window addTitlebarAccessoryViewController:dummyTitlebarAccessoryViewController];


}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接