swiftnsstatusitemnsstatusbar

Replacing NSStatusBarButton NSView to disable vibrancy


I am currently trying to reduce the default vibrancy that is added to NSStatusBarItems icons in Mac OS. I understand this can be achieved by changing the allowsVibrancy attribute. However, since that is read-only on NSView, I need to extend that class and override the allowsVibrancy method.

class NoneVibrancyView: NSView
{
    override var allowsVibrancy: Bool { return false }
}

Currently my icon is still looking like allowsVibrancy is activated. I am wondering if Im replacing my NSView correctly. Here is my code:

let statusBar = NSStatusBar.system

statusBarItem = statusBar.statusItem(withLength: NSStatusItem.squareLength)

let noneVibrancyView = NoneVibrancyView(frame: statusBarItem.button!.frame)

statusBarItem.button?.addSubview(noneVibrancyView)

statusBarItem.button?.title = "😀"

let statusBarMenu = NSMenu(title: "My Status Menu Bar")

statusBarItem.menu = statusBarMenu

Solution

  • Vibrancies of subviews will be overridden by its parent's vibrancy. Therefore, it is required to set the vibrancy of the status bar button that is parent of your custom view (due to addSubview()).

    Since the button of the status item is automatically generated, you may have to use extensions for that:

    extension NSStatusBarButton { 
        override open var allowsVibrancy: Bool { return false } 
    }