部分解决方案
为 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())
我没有弄清楚如何更改背景颜色或如何进行重大定制。我确信我们应该在我改变颜色的动作对象上工作。