Change color of title bar in cocoa

2019-02-15 18:12发布

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?

4条回答
一夜七次
2楼-- · 2019-02-15 18:58

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.

查看更多
Root(大扎)
3楼-- · 2019-02-15 18:59

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

查看更多
干净又极端
4楼-- · 2019-02-15 19:00

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];

查看更多
We Are One
5楼-- · 2019-02-15 19:04

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.

查看更多
登录 后发表回答