简体   繁体   English

如果出现警报/工作表,则制作 SwiftUI 视图灰度

[英]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)
    }
}

Result结果

结果的 GIF

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SwiftUIpresentationMode:检查视图是否按工作表呈现 - SwiftUI presentationMode: Check if view is presented by sheet SwiftUI:如何确定视图是在 NavigationView、Sheet 中呈现还是在根目录中? - SwiftUI: How can I determine if a view is presented in a NavigationView, Sheet, or is the root? 如何在 SwiftUI 中显示为 sheet/fullscreenCover 的嵌套视图中禁用可刷新? - How to disable refreshable in nested view which is presented as sheet/fullscreenCover in SwiftUI? 如何使用 SwiftUI 将 CoreData 上下文传递给在 Swift 5 中作为工作表呈现的新视图? - How to pass CoreData Context to new View presented as a Sheet in Swift 5 using SwiftUI? SwiftUI 无法关闭提供的第二张工作表 - SwiftUI cannot dismiss the second sheet presented SwiftUI 列表重绘已提交工作表的内容 - SwiftUI List redraw the content of a presented sheet 视图在 iOS 14 上的 SwiftUI 中呈现两次 - View presented twice in SwiftUI on iOS 14 是否可以在 SwiftUI 中应用灰度图像作为视图蒙版? - Is it possible to apply a grayscale image as view mask in SwiftUI? 在已提交的表单上以当前模式查看 - present modally view on already presented form sheet 出现操作表时,View Controller不会自动旋转 - View controller is not autorotating when Action Sheet is presented
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM