Images and Colors Not Loading from Xcode Assets Catalog when Making Screensaver for MacOS in Swift/SwiftUI

265 Views Asked by At

I am trying to make a screensaver for my Mac using Swift and SwiftUI. Unfortunately, I cannot seem to load any of the images or colors from my asset catalogue and seek your wisdom on how to remedy this.

I've seen some answers saying that you have to load the image from a different bundle than main and that's why the catalog doesn't work but honestly I'm stumped. Any help is greatly appreciated and here is my pared down code.

class MainView: ScreenSaverView {
    override func draw(_ rect: NSRect) {
        super.draw(rect)
        
        let controller = NSHostingController(rootView: ContentView())
        self.addSubview(controller.view)
        
        controller.view.translatesAutoresizingMaskIntoConstraints = false
        
        let constraints = [
            controller.view.topAnchor.constraint(equalTo: self.topAnchor),
            controller.view.leftAnchor.constraint(equalTo: self.leftAnchor),
            self.bottomAnchor.constraint(equalTo: controller.view.bottomAnchor),
            self.rightAnchor.constraint(equalTo: controller.view.rightAnchor)
        ]
        
        NSLayoutConstraint.activate(constraints)
    }
    ...
}

struct ContentView: View {
    var body: some View {
        VStack(spacing: 10) {
            Text("Working")
                .foregroundColor(Color("FunColor"))
            
            Image("BlueImage")
                .resizable()
                .scaledToFill()
                .frame(width: 190, height: 60)
        }
        .padding()
    }
}
0

There are 0 best solutions below