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

时间:2019-08-04 14:31:32

标签: swift swiftui

我正在尝试在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)
}

此后,我还尝试更改显示此操作表但不起作用的视图的强调色。

2 个答案:

答案 0 :(得分:7)

我已通过在SceneDelegate内的func scene(_...中添加以下代码来使此工作正常进行

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

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

答案 1 :(得分:0)

针对 iOS 14 更新

基于吉安的回答。

第 1 步:转到 Assets.xcassets 并在 Colors 文件夹中添加名为 myCustomColor

的自定义颜色

第 2 步。您需要为 Color 编写扩展。我在 DesignSystem.swift 中添加了这些东西,我为我使用的所有自定义字体和颜色创建。

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

步骤 3. 在 SceneDelegate 中的 func scene(_...

中添加以下代码
UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = UIColor(Color.myCustomColor)