11

I am trying to change color of actionSheet's text in SwiftUI. Unable to do so. I tried following code

    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)
}

After that I also tried to change the accent color of the view in which I am showing this action sheet but its not working.

Lalli
  • 436
  • 5
  • 12

2 Answers2

12

I've managed to get this working by adding the following code in SceneDelegate inside func scene(_...

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

This will change the accent colour of all ActionSheets in your application.

Gian
  • 193
  • 3
  • 9
1

Updated for iOS 14

Based on Gian's answer.

Step 1. Go to Assets.xcassets and in Colors folder add your custom color with a name myCustomColor

Step 2. You need to write an extension to Color. I add such things in DesignSystem.swift I create for all the custom fonts and colors I am using.

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

Step 3. Add the following code in SceneDelegate inside func scene(_...

UIView.appearance(whenContainedInInstancesOf: [UIAlertController.self]).tintColor = UIColor(Color.myCustomColor)
mallow
  • 2,368
  • 2
  • 22
  • 63