kdion4891
kdion4891

Reputation: 1267

UINavigationBar foregroundColor use Color instead of UIColor

How do I use a Color instead of a UIColor for my navigation bar text?

Example, this works:

init() {
    UINavigationBar.appearance().largeTitleTextAttributes = [
        .foregroundColor: UIColor.red
    ]
}

This does not:

init() {
    UINavigationBar.appearance().largeTitleTextAttributes = [
        .foregroundColor: .red
    ]
}

How do I do this properly? UIColor.red is much uglier than Color.red

Upvotes: 0

Views: 72

Answers (2)

kdion4891
kdion4891

Reputation: 1267

So I just found out UIColor.systemRed is the same as Color.red...

Upvotes: 1

David Thorn
David Thorn

Reputation: 129

var largeTitleTextAttributes: [NSAttributedString.Key : Any]? { get set }

As you can see the dictionary is [NSAttributedString.Key : Any], this means that .red cannot be inferred by the compiler, which is why you have to do, UIColor.red.

The .foregroundColor works just fine because it is expecting a NSAttributedString.Key.

I am afraid to say that you have to be explicit in this situation.

Upvotes: 2

Related Questions