gpt4 book ai didi

iOS SwiftUI : ActionSheet custom style

转载 作者:行者123 更新时间:2023-11-29 05:20:22 25 4
gpt4 key购买 nike

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

这是我的actionSheet的代码:

.actionSheet(isPresented: $viewModel.isCustomItemSelected) {
ActionSheet(
title: Text("Add Item"),
message: Text("Which item would you like to add?"),
buttons: [
.default(Text("Todo")),
.default(Text("Event")),
.cancel(Text("Cancel"))
])
}

无论我尝试什么,比如色调颜色、前景色等。它都不会改变颜色。正确的改变方法是怎样的呢?我想 SwiftUi 没有任何 API 来设置它的样式,但我确信这应该是任何解决方法。

最佳答案

部分解决方案

为 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
})
}
}

比在 View 中,在 .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())

我不知道如何更改背景颜色或如何进行重大定制。我确信我们应该处理要更改颜色的操作对象。

关于iOS SwiftUI : ActionSheet custom style,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58728616/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com