Jane Trifels
Jane Trifels

Reputation: 185

Wrong AccentColor showing, color ignored

I added a non-blue color named AccentColor to my iOS app’s assets catalogue. When running my app the tint color is default blue.

The “Global Accent Color Name” in build settings is correctly set to “AccentColor”. Do I need to set anything else? What setting could override this?

Upvotes: 10

Views: 2227

Answers (4)

VladislavSmolyanoy
VladislavSmolyanoy

Reputation: 526

.accentColor() is deprecated so I have no idea why people keep recommending it.

In my case, it was setting the UINavigationBar font, in init() of my SwiftUI app. This for some stupid reason caused the accent color binding that is set in Assets and Build Phases to break and not work on many UI elements.

What fixed it for me is to set the textAttributes of UINavigationBar in init() of ContentView(), which is rendered after WindowGroup appears.

Breaks Accent Color Binding:

// MainApp.swift

@main
struct MainApp: App {
  init() {
     // [...]
     UINavigationBar.appearance().largeTitleTextAttributes = [.font: roundedLargeTitleFont]
  }

  var body: some Scene {
    WindowGroup {
      ContentView()
    }
   }
}

What worked instead:

// ContentView.swift

struct ContentView: View {
  init() {
     // [...]
     UINavigationBar.appearance().largeTitleTextAttributes = [.font: roundedLargeTitleFont]
  }

  var body: some View {
    // My top layer view in the hierarchy, so the above init works as expected. 
  }
}

Upvotes: 0

Pedro Schott
Pedro Schott

Reputation: 1

I found the same issue recently, and I suspect it was because I installed Firebase package.

A quick fix is to override in the main entry point of your app:

var body: some Scene {
        WindowGroup {
            RootView()
                // ... 
                .accentColor(.white)
        }
    }

Hope it works for you

Upvotes: 0

sergej shafarenka
sergej shafarenka

Reputation: 20426

Not sure if the reason for your issue the same as mine, but I suddenly stumbled upon the same effect. Although AccentColor is defined in Assets and is bound to "Global Accent Color Name", previews and app felt back to standard blue color.

It turned out I instantiated a type extending UIViewController before calling WindowGroup. Even though the instance has not been used anywhere, it was enough to just instantiate it to break binding to "Global Accent Color Name". After postponing instantiation of the type to after WindowGroup is called, the accent color binding started to work again.

Upvotes: 4

Jordan H
Jordan H

Reputation: 55845

I encountered this same issue in my app. It used to work but at some point all controls became tinted the default blue instead of my custom color. I confirmed Global Accent Color Name is correct in the target's build settings and that asset catalog is added to that target.

After a bunch of debugging, I found the cause. This line of code breaks the global accent color (tested with Xcode 14 beta 5):

UINavigationBar.appearance().largeTitleTextAttributes = [.font : UIFont(descriptor: UIFontDescriptor.preferredFontDescriptor(withTextStyle: .largeTitle).withDesign(.rounded)!.withSymbolicTraits(.traitBold)!, size: 34)]

However, something like this does not break it:

UINavigationBar.appearance().largeTitleTextAttributes = [.font: UIFont.systemFont(ofSize: 34)]

Super odd. If you have any UINavigationBar appearance overrides, try commenting them out to see if that's causing your issue.

And as a workaround, what I'm doing is setting the window's tintColor in the SceneDelegate (manually added for my SwiftUI app):

final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
    
    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        (scene as? UIWindowScene)?.keyWindow?.tintColor = UIColor(named: "AccentColor")
    }
    
}

Upvotes: 7

Related Questions