5

I'm trying to change the text color and the background color of the ActionSheet in SwiftUI.

This is the code of my actionSheet:

.actionSheet(isPresented: $viewModel.isCustomItemSelected) {
        ActionSheet(
            title: Text("Add Item"),
            message: Text("Which item would you like to add?"),
            buttons: [
                .default(Text("Todo")),
                .default(Text("Event")),
                .cancel(Text("Cancel"))
        ])
}

And whatever I try, like tint color, foreground color etc.. It does not change the color. How is the correct way to change it? I imagine that SwiftUi does not have any API to style it, but I'm sure that should be any workaround.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Andrea Miotto
  • 7,084
  • 8
  • 45
  • 70

1 Answers1

-1

Partial Solution

Create a custom configurator for the ActionSheet:

import SwiftUI

struct ActionSheetConfigurator: UIViewControllerRepresentable {
    var configure: (UIAlertController) -> Void = { _ in }

    func makeUIViewController(context: UIViewControllerRepresentableContext<ActionSheetConfigurator>) -> UIViewController {
        UIViewController()
    }

    func updateUIViewController(
        _ uiViewController: UIViewController,
        context: UIViewControllerRepresentableContext<ActionSheetConfigurator>) {
        if let actionSheet = uiViewController.presentedViewController as? UIAlertController,
        actionSheet.preferredStyle == .actionSheet {
            self.configure(actionSheet)
        }
    }
}

struct ActionSheetCustom: ViewModifier {

    func body(content: Content) -> some View {
        content
            .background(ActionSheetConfigurator { action in
                // change the text color
                action.view.tintColor = UIColor.black
            })
    }
}

Than in the view, after the .actionSheet modifier add the custom modifier like follow:

.actionSheet(isPresented: $viewModel.isCustomItemSelected) {
        ActionSheet(
            title: Text("Add Item"),
            message: Text("Wich item would you like to add?"),
            buttons: [
                .default(Text("Todo")),
                .default(Text("Event")),
                .cancel(Text("Cancel"))
        ])
    }
    .modifier(ActionSheetCustom())

I did not figure it out how to change the background color or how to make major customisation. I'm sure we should work on the action object where I'm changing the color.

Community
  • 1
  • 1
Andrea Miotto
  • 7,084
  • 8
  • 45
  • 70