Declaration
compose().opacity(opacity: number)nonisolated func opacity(_ opacity: Double) -> some ViewParameters
opacity*numberA 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)
}
}
}See also
More in Appearance.
- backgroundSets the view's background to a style.
- badgeGenerates a badge for the view from a localized string resource.
- badgeProminenceSpecifies the prominence of badges created by this view.
- borderAdds a border to this view with the specified style and width.
- buttonSizingThe preferred sizing behavior of buttons in the view hierarchy.
- containerBackgroundSets the container background of the enclosing container using a view.
- headerProminenceSets the header prominence for this view.
- hiddenHides this view unconditionally.
- imageScaleScales images within the view according to one of the relative sizes available including small, medium, and large images sizes.
- labelIconToTitleSpacingSet the spacing between the icon and title in labels.
- labelReservedIconWidthSet the width reserved for icons in labels.
- labelsHiddenHides the labels of any controls contained within this view.