【问题标题】:iOS SwiftUI: ActionSheet custom styleiOS SwiftUI:ActionSheet 自定义样式
【发布时间】:2020-03-02 20:19:09
【问题描述】:

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

这是我的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 来设置它的样式,但我确信这应该是任何解决方法。

【问题讨论】:

    标签: ios swift swiftui actionsheet


    【解决方案1】:

    部分解决方案

    为 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 对象。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-22
      • 2015-03-22
      • 1970-01-01
      • 2011-08-10
      • 2017-02-10
      • 2011-05-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多