我是SwiftUI的新手,如果暗模式被激活,我会改变警报中“接受”按钮的颜色。在浅色模式下,按钮的颜色应与深色模式下的颜色不同。
struct ContentView: View {
@State var alertShouldBeShown = !UserDefaults.standard.bool(forKey: "FirstStart")
var body: some View {
VStack {
Text("Hello World!")
.alert(isPresented: $alertShouldBeShown, content: {
Alert(title: Text("Disclaimer"),
message: Text("Placeholder"),
dismissButton: Alert.Button.default(
Text("Accept"), action: {
UserDefaults.standard.set(true, forKey: "FirstStart")
}
)
)
})
}
}
}
发布于 2020-09-14 05:19:45
警告按钮的颜色基于应用程序的色调/强调色。对于暗模式和亮模式,此颜色实际上可以是动态的。最简单的方法是在资产目录中定义颜色。
在iOS 14测试版中,您可以在资产目录中定义SwiftUI的强调色,而无需编写任何额外的代码。如果你想让它在iOS 13中工作,你可能应该阅读this的文章。如果要使用资源目录中具有不同亮/暗模式外观的颜色,请使用UIColor(named: "")
应用编程接口。
发布于 2020-09-14 04:47:48
你不能改变内置Alert
上的文字和颜色。您将创建自己的自定义警报。
例如:
struct ContentView: View {
@State private var showAlert = false
var body: some View {
ZStack {
Button(action: { showAlert = true }) {
Text("Press")
}
if showAlert {
Color.black
.opacity(0.1)
CustomAlert(isPresented: $showAlert, title: "Disclaimer", bodyText: "Placeholder", buttonText: "Accept")
}
}
}
}
struct CustomAlert: View {
@Environment(\.colorScheme) private var colorScheme
@Binding var isPresented: Bool
var title: String
var bodyText: String
var buttonText: String
var body: some View {
ZStack {
VStack {
Text(title)
.bold()
Text(bodyText)
Button(action: { self.isPresented = false}) {
Text(buttonText)
.foregroundColor(colorScheme == .light ? .blue : .green)
}
}
.padding(20)
.background(
Color.white
.cornerRadius(10)
)
}
}
}
https://stackoverflow.com/questions/63875176
复制相似问题