'NSImage not loading macOS Swift

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



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source