在 UIKit 和 SwiftUI 中,工作表或警报会在其显示的视图中使标准颜色变暗。在显示警报/工作表期间,该视图中的每种颜色都是灰度的。
如何同时调暗 SwiftUI 视图的非标准颜色?我使用自己的颜色,默认情况下它们不会变暗。
.saturation当给定的警报/工作表可见时,使用修改器使视图变为灰度。
以下示例使用警报。isPresented它利用三元运算符中用于警报参数的 State 变量:
.saturation(isAlertVisible ? 0 : 1)
Run Code Online (Sandbox Code Playgroud)
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)
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
690 次 |
| 最近记录: |