'Why am I getting "Instance method 'background(_:alignment:)' requires that 'UIColor' conform to 'View'"?
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:
Thanks
Solution 1:[1]
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)
Solution 2:[2]
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())
}
}
Solution 3:[3]
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)))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | pawello2222 |
Solution 2 | SudhakarH |
Solution 3 | pkamb |