I'm following the instructions for setting a custom color on AccentColor
in xcassets catalog:
Made sure the build setting was in place:
But none of the buttons in my app are affected by this color.
Instead I need to add this to see it in simulator:
WindowGroup {
ContentView()
.accentColor(.accent)
}
and I need to set this on every preview to see it in the preview system.
Example of button code I'm using, but I've tried every buttonStyle of button/navigationLink, none of them get the AccentColor tint color automatically, I still have to set this manually on every button:
Button {
} label: {
Image(systemName: "plus")
.font(.system(size: 40))
}
.buttonStyle(.borderedProminent)
.clipShape(Capsule())
I assumed the whole point of this asset was so I wouldn't need to do this? Is this a known issue or am I doing this wrong?
Thanks to @Sweeper for his comments helping me with this.
I was able to track down what was causing this by creating a new project (which showed the global accent color fine) and slowly applying my current project's code to it.
The cause is something really crazy... seems to have to do with setting a Data value on a SwiftData property during initialization.
Here's the most basic example:
Code for @main app:
@main
struct test_accent_colorApp: App {
var appManager = AppManager()
var body: some Scene {
WindowGroup {
ContentView()
}
.modelContainer(appManager.testDataModelContainer)
}
}
Basically creating an AppManager class which creates some dummy SwiftData preview content.
App Manager class:
@Observable class AppManager {
var testDataModelContainer: ModelContainer = previewContainer
}
All this does is initialize a test data container.
Preview container code:
@MainActor
let previewContainer: ModelContainer = {
do {
let container = try ModelContainer(
for: Item.self,
configurations: ModelConfiguration(isStoredInMemoryOnly: true)
)
let item = Item(timestamp: Date())
container.mainContext.insert(item)
return container
} catch {
fatalError("Failed to create container \(error)")
}
}()
This is a global variable, just creates a single Item entry with the current date, nothing else.
Item Model:
@Model
final class Item {
var timestamp: Date
var image: Data?
init(timestamp: Date) {
self.timestamp = timestamp
//Global tint works
self.image = nil
//Global tint doesn't work
// self.image = UIImage(named: "placeholder.jpg")!.jpegData(compressionQuality: 1.0)!
}
}
Here, it has just 2 properties. The Data? property is what causes Gobal Tint to break down.
Here is when I initialize the item.image to nil, global tint works:
Here is when I initialize the item.image with an image from the app bundle:
Link to sample project zip file if anyone is curious.