13

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.

3
  • Is "GreenColor" a custom color? Commented Aug 5, 2019 at 2:36
  • Yes this was from assets Commented Aug 5, 2019 at 17:20
  • Have you been able to accomplish this? I am having the same issue. Commented Jan 31, 2020 at 19:18

2 Answers 2

14

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.

Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.