Swift MACos Popover Detect Change Dark Mode

How to detect switch between macOS default & dark mode using Swift 3

I'm using this Swift 3 syntax successfully:

DistributedNotificationCenter.default.addObserver(self, selector: #selector(interfaceModeChanged(sender:)), name: NSNotification.Name(rawValue: "AppleInterfaceThemeChangedNotification"), object: nil)

func interfaceModeChanged(sender: NSNotification) {
...
}

Disable dark mode for mac OSX App in Swift

You'll need initialize the name first:

popover.appearance = NSAppearance(named: NSAppearanceNameAqua)

You were close.

Edit for Swift 4.1:

popover?.appearance = NSAppearance(named: NSAppearance.Name.aqua)

SwiftUI macOs AppDelegate detect the change of ObservedObject

There is a more sophisticated way to observe UserDefaults.

The Combine framework provides a publisher for UserDefaults, which is actually a Key-Value Observing Publisher

First make your backgroundIsTransparent property an extension of UserDefaults

extension UserDefaults {
@objc var backgroundIsTransparent: Bool {
get {
guard object(forKey: PreferencesKeys.backgroundIsTransparent) != nil else { return true }
return bool(forKey: PreferencesKeys.backgroundIsTransparent)
}
set {
set(newValue, forKey: PreferencesKeys.backgroundIsTransparent)
}
}
}

In AppDelegate import Combine and create a Set for the subscription(s)

import Combine

@main
class AppDelegate: NSObject, NSApplicationDelegate { ...

private var subscriptions = Set<AnyCancellable>()

and in applicationDidFinishLaunching add the publisher

    func applicationDidFinishLaunching(_ aNotification: Notification) {

// .....

UserDefaults.standard.publisher(for: \.backgroundIsTransparent)
.sink { state in
print(state)
// do something with state
}
.store(in: &subscriptions)
}

An alternative using your approach is to make userPreferences a @StateObject

@StateObject var userPreferences = UserPreferences.instance

and observe backgroundIsTransparent

userPreferences.$backgroundIsTransparent
.sink { state in
print(state)
// do something with state
}
.store(in: &subscriptions)

Swift ui macOS menuBar NSPopover no arrow

There is no public API for it I believe, try this instead:

popover.setValue(true, forKeyPath: "shouldHideAnchor")

Output:

Sample Image

How to change NSPopover background color include triangle part?

Unfortunately, this isn't really possible. NSPopover wasn't designed to be customizable. If you want a completely customized appearance, your best bet is probably to use a third-party, open-source NSPopover replacement like INPopoverController.

Change UIPopoverView background + arrow color

I found the solution. Subclassing is not necessary anymore with iOS8! The background can be accessed and changed like this from within the tableview -> navigation -> popoverPresentationController

    self.navigationController?.popoverPresentationController?.backgroundColor = UIColor.redColor()

More information about this in WWDC session 2014.



Related Topics



Leave a reply



Submit