iOS SwiftUI:ActionSheet自定义样式

时间:2019-11-06 11:01:45

标签: ios swift swiftui actionsheet

我正在尝试在SwiftUI中更改ActionSheet的文本颜色和背景颜色。

这是我的actionSheet的代码:

jdbc:mysql://{mysql_server}/confluence?useSSL=true&sslMode=VERIFY_IDENTITY&trustCertificateKeyStoreUrl=file%3A%2Fetc%2Fssl%2Fcerts%2Fjava%2Fcacerts&trustCertificateKeyStorePassword=changeit&enabledTLSProtocols=TLSv1.2&useUnicode=true&characterEncoding=utf8

无论我尝试什么,例如色调颜色,前景色等。它都不会改变颜色。 如何更改它的正确方法? 我想象SwiftUi没有任何样式的API,但是我确定应该是任何解决方法。

1 个答案:

答案 0 :(得分:0)

部分解决方案

为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 对象上进行操作。