如何在SwiftUI中更改ActionSheet的文本颜色

11
我正在尝试在SwiftUI中更改actionSheet文本的颜色,但无法做到。我尝试了以下代码。

我正在尝试在SwiftUI中更改actionSheet文本的颜色,但无法做到。我尝试了以下代码。

    private var actionSheet: ActionSheet {
    let button1 = ActionSheet.Button.default(Text("Week 1").foregroundColor(.orange)) {
        self.selectedShuffleWeek = "Week 1"
        self.shufflePresented = true
        self.actionSheetPresented = false
    }
    let button2 = ActionSheet.Button.default(Text("Week 2").foregroundColor(Color("GreenColor"))) {
        self.selectedShuffleWeek = "Week 2"
        self.shufflePresented = true
        self.actionSheetPresented = false
    }
    let button3 = ActionSheet.Button.default(Text("Week 3").foregroundColor(Color("GreenColor"))) {
        self.selectedShuffleWeek = "Week 3"
        self.shufflePresented = true
        self.actionSheetPresented = false
    }
    let dismissButton = ActionSheet.Button.cancel {
        self.selectedShuffleWeek = ""
        self.actionSheetPresented = false
    }


    let buttons = [button1, button2, button3, dismissButton]
    return ActionSheet(title: Text("Shuffle"),
                       buttons: buttons)
}

之后,我也尝试更改显示此操作表的视图的重点颜色,但没有成功。


"GreenColor" 是自定义颜色吗? - Yuto
是的,这是来自资产。 - Lalli
你能完成这个任务吗?我也遇到了同样的问题。 - Ferdinand Rios
2个回答

12

我通过在SceneDelegate中添加以下代码(在func scene(_...内部)使其工作:

UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = Color("GreenColor")

这将更改应用程序中所有操作表的强调颜色。


1

iOS 14 更新

根据 Gian 的回答。

步骤 1. 进入 Assets.xcassets,在 Colors 文件夹中添加名称为 myCustomColor 的自定义颜色。

步骤 2. 您需要编写一个 Color 扩展。我将这些内容添加到 DesignSystem.swift 中,该文件用于存储我使用的所有自定义字体和颜色。

extension Color {
    public static let myCustomColor = Color("myCustomColor")
}

步骤三:在SceneDelegate中的func scene(_...中添加以下代码。

UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = UIColor(Color.myCustomColor)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接