Search code examples
swiftswiftui

Color rgb initializer not working as background in SwiftUI


I'm trying to set a button's background color to some custom rgb value. I create the button as follows:

Button(action: {
    print("tapped")
}) {
    Text("Let's go")
}
    .background(Color.black)

This works fine, and the button's background is, in fact, black. However, when initializing the background color like this, it does not work and there's just no background color at all:

.background(Color(red: 242, green: 242, blue: 242))

Why is that?


Solution

  • It looks like it is asking for the colors in percentages, I was able to get it to work doing this

    Color(red: 242 / 255, green: 242 / 255, blue: 242 / 255)