iOS SwiftUI:操作表自定义样式
我正在尝试在SwiftUI中更改操作表的文本颜色和背景颜色 这是我的行动计划的代码:iOS SwiftUI:操作表自定义样式,ios,swift,swiftui,actionsheet,Ios,Swift,Swiftui,Actionsheet,我正在尝试在SwiftUI中更改操作表的文本颜色和背景颜色 这是我的行动计划的代码: .actionSheet(isPresented: $viewModel.isCustomItemSelected) { ActionSheet( title: Text("Add Item"), message: Text("Wich item would you like to add?"), buttons: [
.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"))
])
}
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
})
}
}
无论我尝试什么,比如浅色,前景色等等。。它不会改变颜色。
如何正确地改变它?
我想SwiftUi没有任何API来设计它的样式,但我确信这应该是一个解决方案。部分解决方案
为操作表创建自定义配置程序:
.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"))
])
}
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
})
}
}
我没有弄清楚如何改变背景颜色,或者如何进行重大定制。我确信我们应该在我改变颜色的动作对象上工作