Instance Method

opacity(_:)

Sets the transparency of the view.

Declaration

func opacity(_ opacity: Double) -> some View

Parameters

opacity

A value between 0 (fully transparent) and 1 (fully opaque).

Return Value

A view that sets the transparency of this view.

Discussion

Apply opacity to reveal views that are behind another view or to de-emphasize a view.

When applying the opacity(_:) modifier to a view that has already had its opacity transformed, the modifier multiplies the effect of the underlying opacity transformation.

The example below shows yellow and red rectangles configured to overlap. The top yellow rectangle has its opacity set to 50%, allowing the occluded portion of the bottom rectangle to be visible:

struct Opacity: View {
    var body: some View {
        VStack {
            Color.yellow.frame(width: 100, height: 100, alignment: .center)
                .zIndex(1)
                .opacity(0.5)

            Color.red.frame(width: 100, height: 100, alignment: .center)
                .padding(-40)
        }
    }
}
Two overlaid rectangles, where the topmost has its opacity set to 50%, which allows the occluded portion of the bottom rectangle to be visible.

See Also

Applying Graphical Effects to a View

func blur(radius: CGFloat, opaque: Bool) -> View

Applies a Gaussian blur to the view.

func brightness(Double) -> View

Brightens the view by the specified amount.

func contrast(Double) -> View

Sets the contrast and separation between similar colors in this view.

func colorInvert() -> View

Inverts the colors in this view.

func colorMultiply(Color) -> View

Adds a color multiplication effect to this view.

func saturation(Double) -> View

Adjusts the color saturation of this view.

func grayscale(Double) -> View

Adds a grayscale effect to this view.

func hueRotation(Angle) -> View

Applies a hue rotation effect to this view.

func luminanceToAlpha() -> View

Adds a luminance to alpha effect to this view.