Use the .saturation
modifier to make your view grayscale when a given alert/sheet is visible.
The following example uses an alert. It utilizes the State variable used for the alert's isPresented
parameter in the ternary operator:
.saturation(isAlertVisible ? 0 : 1)
struct ContentView: View {
@State var isAlertVisible = false
var body: some View {
VStack {
Text("Text")
.foregroundColor(.red)
Spacer()
.frame(height: 300)
Button {
isAlertVisible = true
} label: {
Text("Button")
}
.alert(isPresented: $isAlertVisible) {
Alert(
title: Text("Alert"),
message: Text("Message"),
dismissButton: Alert.Button.default(Text("OK"))
)
}
}
.saturation(isAlertVisible ? 0 : 1)
}
}
Result
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…