Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I getting "Instance method 'background(_:alignment:)' requires that 'UIColor' conform to 'View'"?

Tags:

swiftui

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

like image 488
David Avatar asked Dec 30 '20 16:12

David


2 Answers

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)
like image 115
pawello2222 Avatar answered Oct 17 '22 15:10

pawello2222


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)))
like image 30
Albert Bůžek Avatar answered Oct 17 '22 15:10

Albert Bůžek