gpt4 book ai didi

swift - 如何更改应用程序色调颜色(新的 SwiftUI 生命周期应用程序)?

转载 作者:行者123 更新时间:2023-12-04 13:12:13 49 4
gpt4 key购买 nike

在 SwiftUI 应用程序中更改应用程序色调颜色的最佳方法是什么?
它由新的 SwiftUI 生命周期提供支持,因此我无法选择执行 self.?tintColor
另外,我该如何实现这个菜单?
enter image description here
(猜猜它正在使用选择器和表单,但我无法在颜色名称旁边添加圆圈)
尝试在此处搜索,但在 SwiftUI 生命周期应用程序中找不到任何方法。

最佳答案

SceneDelegate.swift在为应用程序创建窗口的地方,您可以使用 tintColor 全局设置色调颜色UIWindow 的属性(property)

let contentView = ContentView()

if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window

self.window?.tintColor = UIColor.red // Or any other color you want
window.makeKeyAndVisible()
}
编辑
在看到你想要它用于新的 SwiftUI 之后,你可以创建新的 EnvironmentKeys:
private struct TintKey: EnvironmentKey {
static let defaultValue: Color = Color.blue
}

extension EnvironmentValues {
var tintColor: Color {
get { self[TintKey.self] }
set { self[TintKey.self] = newValue }
}
}

@main
struct YourApp: App {
var body: some Scene {
WindowGroup {
ContentView().environment(\.tintColor, Color.red)
}
}
}
然后在您的 View 中,您将像这样使用它:
struct ContentView: View {
@Environment(\.tintColor) var tintColor

var body: some View {
VStack {
Text("Hello, world!")
.padding()
Button(action: {}, label: {
Text("Button")
})
}.accentColor(tintColor)
}
}

关于swift - 如何更改应用程序色调颜色(新的 SwiftUI 生命周期应用程序)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64065134/

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