0

I have a config file and I want to change colors there. But in my view function background(bg1Color) gives me error;

Argument type 'UIColor' does not conform to expected type 'View'

How can I fix this?

My config file;

import UIKit
import Foundation

// Backgrounds
var bg1Color: UIColor = .black

My function codes;

.background(bg1Color) // HERE
.cornerRadius(10)
1

2 Answers 2

4

.background modifier takes a View and unlike SwiftUI's Color, UIKit's UIColor is not conformed to View protocol (by default).

So you need to create a Color from your UIColor and then pass it to the .background modifier.

.background(Color(bg1Color))

or directly:

.background(Color(.black))

Very Important Note!

UIColor and Color predefined named colors are not the same! And they have different shades!

Example

So you should NOT just replace UIColor with Color.

Actually SwiftIUI.Color.red quals to UIKit.UIClor.systemRed

Sign up to request clarification or add additional context in comments.

4 Comments

Wait... what? I never realised the SwiftUI.Color and UIColor did not make the same shades. Is this true if you use RGB initialiser too?
I meant for predefined named colors as I mentioned in the image. If you manually pass the values, they will be the same of course.
Ah, ok. Haha, that makes more sense. For a second there I thought you meant they were using different colour spaces. 😂
Actually SwiftIUI.Color.red quals to UIKit.UIClor.systemRed
1

It's Color not UIColor

 import SwiftUI

 var bg1Color : Color = .black

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.