iOS SwiftUI: пользовательский стиль ActionSheet - PullRequest
0 голосов
/ 06 ноября 2019

Я пытаюсь изменить цвет текста и цвет фона ActionSheet в SwiftUI.

Это код моего actionSheet:

.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"))
        ])
}

И что бы я ни пытался,как цвет оттенка, цвет переднего плана и т. д. Это не меняет цвет. Как правильно это изменить? Я полагаю, что SwiftUi не имеет API для стилизации, но я уверен, что это должно быть обходным путем.

1 Ответ

0 голосов
/ 06 ноября 2019

Частичное решение

Создайте настраиваемый конфигуратор для 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
            })
    }
}

Чем в представлении после модификатора .actionSheet добавить настраиваемый модификатор, как следует:

.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())

Я не понял, как изменить цвет фона или как сделать большую настройку. Я уверен, что мы должны поработать над объектом action , где я меняю цвет.

...