Fill color affects shadow in SwiftUI

Viewed 52

I have a SwiftUI view that applies a fill and shadow to a shape like this:

struct ShadowRectangle: View {
    let color: UIColor
    var body: some View {
        Rectangle()
            .fill(Color(color))
            .shadow(color: .red, radius: 5, x: 0, y: 0)
    }
}

I have found that the fill color I use for this view can affect the way the shadow is drawn. For example:

VStack {
    ShadowRectangle(color: .white)
    ShadowRectangle(color: .quaternaryLabel)
}

will look like

two rectangles generated by the code above

What's going on here? How can I use UIColor.quaternaryLabel without messing up the shadow applied to the shape?

1 Answers

Try using the UIColor(red:green:blue:alpha) initializer (the value is .quaternaryLabel converted to RGB):

ShadowRectangle(color: UIColor(red: 0.863, green: 0.863, blue: 0.867, alpha: 1))
Related