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)
Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
SwiftDeveloper
  • 7,244
  • 14
  • 56
  • 85
  • https://stackoverflow.com/questions/56437036/swiftui-how-do-i-change-the-background-color-of-a-view – Martin R Dec 29 '19 at 11:00

2 Answers2

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

Mojtaba Hosseini
  • 95,414
  • 31
  • 268
  • 278
1

It's Color not UIColor

 import SwiftUI

 var bg1Color : Color = .black
SwiftDeveloper
  • 7,244
  • 14
  • 56
  • 85
Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87