0

I have this code which successfully shows a system icon in the menu bar when the app is running:

statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
    
    
    if let MenuButton = statusItem?.button{
        
        MenuButton.image = NSImage(systemSymbolName: "gearshape.fill", accessibilityDescription: nil)

        MenuButton.action = #selector(MenuButtonToggle)
        
    }

However if I change the code to use a custom image in my Assets folder:

statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
    
    
    if let MenuButton = statusItem?.button{
        
        MenuButton.image = #imageLiteral(resourceName: "icon_2")
        MenuButton.image?.isTemplate = true

        MenuButton.action = #selector(MenuButtonToggle)
        
    }

UPDATE:

I have also tried Bundle(for: type(of: self)).image(forResource: "icon_2") as an alternative to imageLiteral

It doesn't show anything, it's not the image as I have tried loads and I have even tried icons that were used in other projects that I know works for the menubar. What could've gone wrong?

Even used this to debug if the image is loaded or not:

if let image_url = Bundle.main.url(forResource: "icon", withExtension: "png") {
            print("yes")
        }

yes does show up

Willeke
  • 14,578
  • 4
  • 19
  • 47
Nathan
  • 1,393
  • 1
  • 9
  • 40
  • You should load image from your bundle. Next should be helpful https://stackoverflow.com/a/60418869/12299030. – Asperi Feb 17 '22 at 13:22
  • @Asperi Sorry, forgot to mention, I have tried that too – Nathan Feb 17 '22 at 13:23
  • 1
    Then make sure your image is really present in bundle/assets catalog... no miracle here - if it is not loaded then it is not there. – Asperi Feb 17 '22 at 13:31
  • @Asperi which it is so still very confused to why this doesn't work – Nathan Feb 17 '22 at 14:33
  • Is `image` `nil`? Is the question about the status item or about loading the image? Please start variable names with a lower case character. I keep thinking `MenuButton` is a class. – Willeke Feb 17 '22 at 15:37
  • @Willeke no as you can see by the question it is loaded. And sorry I code differently to you, not relevant to the question – Nathan Feb 17 '22 at 16:26

0 Answers0