Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shadow opacity swiftUI

Tags:

swiftui

I have an image with a border around it to which I would like to add a shadow with some offset. Although I think the default opacity of the shadow is too dark, what's the correct property for opacity?

var body: some View {

    Image("football")

        .resizable()
        .scaledToFit()
        .frame(width: 100.0, height: 100.0)
        .clipShape(Circle())
        .overlay(Circle()
        .stroke(Color.white, lineWidth: 4))
        .shadow(radius: 10.0, x: -10.0, y: -10.0)

}
like image 768
Crashtor Avatar asked Feb 29 '20 16:02

Crashtor


People also ask

How do I change opacity in SwiftUI?

Any SwiftUI view can be partially or wholly transparent using the opacity() modifier. This accepts a value between 0 (completely invisible) and 1 (fully opaque), just like the alpha property of UIView in UIKit.


2 Answers

You can pass a Color with reduced opacity to your shadow:

.shadow(color: Color.black.opacity(0.2), radius: 10.0, x: -10.0, y: -10.0)

Note: The default shadow color is a black with 0.33 opacity

Color(.sRGBLinear, white: 0, opacity: 0.33)

like image 71
Alladinian Avatar answered Jan 03 '23 17:01

Alladinian


You can control the shadow by changing the value of X position and Y position.

Text("Hello")
      .frame(width: 100, height: 100)
      .background(Color.red)
      .shadow(color: Color.black.opacity(0.3), radius: 5, x: -15.5, y: 0.0)
      .shadow(color: Color.black.opacity(0.3), radius: 5, x: 15.0, y: 0.0)
like image 31
Imran0001 Avatar answered Jan 03 '23 18:01

Imran0001