Ale*_*res 5 state tintcolor ios swift swiftui
因此,我在我的应用程序中支持三个主题,每个主题都有不同的色调颜色。我正在使用 @EnvironmetObject 来跟踪更改。但是,我无法在 SceneDelegate.swift 文件上使用它,因为应用程序崩溃了。此外,accentColor不是一个选项,因为它不会改变警报tintColor。我该怎么做?
这是一些代码:
SceneDelegate.swift 文件
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
@EnvironmentObject var userData: UserData
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
// Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
// If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
// This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
// Create the SwiftUI view that provides the window contents.
let contentView = TasksView()
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: contentView.environmentObject(UserData()))
self.window = window
window.makeKeyAndVisible()
window.tintColor = userData.selectedTheme.tintColor
}
}
Run Code Online (Sandbox Code Playgroud)
当应用程序启动时,这种方法将会崩溃,因为它无法在其祖先中找到@EnvironmentObject。
ContentView.swift 文件
struct ContentView: View {
@EnvironmentObject var userData: UserData
var body: some View {
NavigationView{
List(userData.tasks) { task in
TaskRow(taskTitle: task.title, taskDetail: task.detail)
}
.navigationBarTitle(Text("Tasks"), displayMode: .automatic)
.navigationBarItems(
leading: NavigationLink(destination: SettingsView(), label: {
Image(systemName: "gear").imageScale(.large)
}),
trailing: NavigationLink(destination: AddTaskView(), label: {
Image(systemName: "plus").imageScale(.large)
})
)
}.navigationViewStyle(StackNavigationViewStyle())
.accentColor(userData.selectedTheme.accentColor)
}
}
Run Code Online (Sandbox Code Playgroud)
这种方法对我来说也不起作用,因为它不会改变警报的色调颜色等。
图片
如果我使用accentColor,这就是我得到的结果
这就是我想要实现的目标
更新:将此演示发布到 GitHub - DemoWindowTint
下面的演示是使用如何在 SwiftUI 视图中访问自己的窗口?tintColor中提供的方法在设置窗口(由所有子视图继承)时创建的。。
在演示中,我使用了NavigationView几个NavigationLinks并Button显示了Alert。

测试如下
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
var window: UIWindow?
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
let contentView = ContentView()
.environment(\.hostingWindow, { [weak window] in
return window })
window.rootViewController = UIHostingController(rootView: contentView)
self.window = window
...
struct ContentView: View {
@Environment(\.hostingWindow) var hostingWindow
... // body can be any
.onAppear {
// can be loaded from UserDefaults here, and later changed on any action
self.hostingWindow()?.tintColor = UIColor.red
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4253 次 |
| 最近记录: |