[英]Make SwiftUI view grayscale if an alert/sheet is presented
In UIKit and SwiftUI, a sheet or an alert dims standard colors in the view on top of which they were shown.在 UIKit 和 SwiftUI 中,工作表或警报在显示它们的顶部视图中使标准 colors 变暗。 Every color in that view is grayscale during the presentation of an alert/sheet.
在显示警报/工作表期间,该视图中的每种颜色都是灰度。
How can I dim my SwiftUI view's non-standard colors as well?我怎样才能使我的 SwiftUI 视图的非标准 colors 变暗? I'm using my own colors and they do not dim by default.
我使用的是我自己的 colors,默认情况下它们不会变暗。
Use the .saturation
modifier to make your view grayscale when a given alert/sheet is visible.当给定的警报/工作表可见时,使用
.saturation
修饰符使您的视图变为灰度。
The following example uses an alert.以下示例使用警报。 It utilizes the State variable used for the alert's
isPresented
parameter in the ternary operator:它利用 State 变量用于三元运算符中警报的
isPresented
参数:
.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)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.