Change color of title bar in cocoa

2019-02-15 18:22发布

问题:

This must have been asked before, but after Googling I still can't find the answer.

How do you change the color of the title bar (The bar that you can click and drag around with the close, minimize and maximize buttons) to a different color than the default gray in Cocoa?

回答1:

If you set the background color of a "textured" window (a distinction that isn't really all that visible in Snow Leopard) that color will be applied to the titlebar as well. This is what Firefox does.

I would recommend though not having a real titlebar (i.e. setting your window to have no titlebar) and using +[NSWindow standardWindowButton:forStyleMask:] and putting your own buttons in the "titlebar". This allows you more control and is way way less hacky.



回答2:

If it's a panel, you can change it to black by instantiating it as a HUD window.

Otherwise, you can't. Ever notice how there aren't any Aqua windows with different-colored title bars roaming around in other apps? This is why.

The only other way to change the appearance of the title bar (without relying on private implementation details such as the existence of a frame view) is to make the window borderless and create its title bar and window buttons from the ground up.



回答3:

If you go with Colin's approach of making the window textured in interface builder (check box in the attributes of the window), here's the line to change the background color of the window you'd put in this function of the appDelegate.m file

//In this function ---> - (void)applicationDidFinishLaunching:(NSNotification *)aNotification

//type this [_window setBackgroundColor: NSColor.whiteColor];



回答4:

If you don't mind private API, you could subclass NSThemeFrame.