10

I would like to understand why am I having issue with the background()?

Instance method 'background(_:alignment:)' requires that 'UIColor' conform to 'View'

var body: some View {
    
    Button("MY BUTTON") {
        print("the action")
    }
    .padding()
    .background(Color.black)
    .foregroundColor(.white)
    .clipShape(Capsule())
}

UPDATE

Get same thing with this:

enter image description here

Thanks

pawello2222
  • 46,897
  • 22
  • 145
  • 209
David
  • 1,241
  • 20
  • 36

3 Answers3

12

You probably created another struct/class called Color. Xcode syntax highlighting is different for Color.black - this suggests you're not using the SwiftUI Color.

Try this calling it explicitly:

.background(SwiftUI.Color.black)
pawello2222
  • 46,897
  • 22
  • 145
  • 209
2

Had the same problem, none of the answers helped me either. What finally worked for me was defining (initialising) the color each time.

Text("Hello World").padding.background(Color.init(UIColor(red: 0, green:0, blue:0, alpha: 1)))
pkamb
  • 33,281
  • 23
  • 160
  • 191
0

Check the syntax once.

This might work:

var body: some View {

    Button(action: {
        print("the action tapped!")
    }) {
        Text("Hello World")
            .padding()
            .background(Color.black)
            .foregroundColor(.white)
            .clipShape(Capsule())
       }
}
SudhakarH
  • 541
  • 5
  • 16