SwiftUI动作为每个动作设置不同的颜色 [英] SwiftUI ActionSheet different color for each action

查看:78
本文介绍了SwiftUI动作为每个动作设置不同的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对SwiftUI中的Actionsheet有疑问.我想创建一个具有2个选项的ActionSheet:删除和取消.删除"按钮以红色和取消"颜色显示绿色.

以下是代码示例:

  Button(操作:{打印(按下删除按钮")self.showingActionSheet = true}){文字(执行操作").foregroundColor(.green).font(.body).填充()}.actionSheet(isPresented:$ showingActionSheet){返回ActionSheet(title:Text("Delete images"),按钮:[.default(Text(删除选定的内容").foregroundColor(.red)){//一些动作},.取消()])} 

问题是动作的颜色是两个按钮的默认颜色(蓝色").我可以通过在"SceneDelegate.swift"中添加以下行来更改此设置甚至上面的代码中.

  UIView.appearance(whenContainedInInstancesOf:[UIAlertController.self]).tintColor = UIColor(名称:绿色") 

此行的问题是它将覆盖从蓝色"到蓝色"的常规颜色.变为绿色".仍然需要找到一种方法来解决如何为每个动作添加不同的颜色.

它是这样的:

  .actionSheet(isPresented:$ showingActionSheet){返回ActionSheet(title:Text("Delete images"),按钮:[.destructive(Text("Delete selected")){//一些动作},.取消()])} 

I have a question regarding Actionsheet in SwiftUI. I want to create an ActionSheet with 2 options: delete and cancel. "Delete" button colored in red and "cancel" in green.

Here is an example of code:

Button(action: {
                    print("Delete button pressed")
                    self.showingActionSheet = true
                }){
                    Text("Go to actions")
                        .foregroundColor(.green)
                        .font(.body)
                        .padding()
                }
                .actionSheet(isPresented: $showingActionSheet) {                   
                    return ActionSheet(title: Text("Delete images"), buttons: [
                        .default(Text("Delete selected").foregroundColor(.red)){
                            // some action to do
                        },
                        .cancel()
                    ])
                }

The problem is that the color for actions is the default one ("blue") for both buttons. I can change this by adding the following line in "SceneDelegate.swift" or even in the code above.

UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = UIColor(named: "green")

The problem with this line is that it will overwrite the general color from "blue" to "green". Still need to find a solution on how to color each action differently.

This is how it looks like: image preview

Do you have any sugestions?

解决方案

There is another button style for actions like delete

.actionSheet(isPresented: $showingActionSheet) {
    return ActionSheet(title: Text("Delete images"), buttons: [
        .destructive(Text("Delete selected")){
            // some action to do
        },
        .cancel()
    ])
}

这篇关于SwiftUI动作为每个动作设置不同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆