Ram Patra
Ram Patra

Reputation: 16664

How to force a view controller to use light or dark mode in macOS?

I know there is a way to programmatically override the interface style in iOS like below:

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        overrideUserInterfaceStyle = .dark    
    }
}

Howover, I tried searching for something similar in macOS but not able to find one. Is there a way to achieve this in macOS?

Upvotes: 7

Views: 1311

Answers (2)

Ram Patra
Ram Patra

Reputation: 16664

I found it finally. You can do the same in macOS like below:

class ViewController: NSViewController { 

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
        view.appearance = NSAppearance(named: .darkAqua)

    }

}

Upvotes: 1

Asperi
Asperi

Reputation: 257493

It can be done via

public protocol NSAppearanceCustomization : NSObjectProtocol {
    @available(OSX 10.9, *)
    var appearance: NSAppearance? { get set }

The NSApplication, NSWindow, and NSView conform to this protocol, so you can use it like

window.appearance = NSAppearance(named: .aqua)
window.makeKeyAndOrderFront(nil)

Upvotes: 4

Related Questions