简体   繁体   中英

How to change the titlebar/toolbar and NSButtons to a dark theme?

I am working on a project for macOS (OS X) where I want the window to be set to a dark theme instead of the default light theme, which has a light gray titlebar/toolbar with white buttons.

How can I change the titlebar/toolbar to a black color and the buttons to a dark gray?

I am coding in Swift but an answer in Objective-C would also be helpful. I have attached an image of what I am aiming to make it look like:

我想要它看起来像什么

Alright, I found the solution.

To change the WindowController to a dark theme, type the following code into the windowDidLoad() function:

window?.appearance = NSAppearance(named: NSAppearanceNameVibrantDark)

This makes the titlebar/toolbar a black color and the buttons a dark gray.

Also, to make the titlebar and toolbar unified into one as shown in the image above, type this code in the windowDidLoad() function along with the above code:

window?.titleVisibility = .hidden

Here is an image of the window when the app is ran: 黑暗主题窗口

I hope this helped those like me who were wondering how to do this.

To call this from inside a ViewController, without a direct outlet for the window:

override func viewDidAppear() {
    super.viewDidAppear()
    self.view.window?.appearance = NSAppearance(named: NSAppearance.Name.vibrantDark)
}

Note that you must call from viewDidAppear() and not viewDidLoad(), as the window will still be nil there.

EDIT: Updated for Swift 4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM