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

8
什么是在SwiftUI应用程序中更改应用程序色调颜色的最佳方法?
它由新的SwiftUI生命周期驱动,因此我没有执行self.?tintColor选项。
尝试在这里搜索,但没有找到在SwiftUI生命周期应用程序中执行此操作的方法。

1
你应该提出一个单一的问题,以便更加专注,并为你的第二个问题创建一个新的问题。 - Itay Brenner
3个回答

10

这个功能不需要 EnvironmentKey,并且能够在应用程序中的所有视图中传播:

@main

struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
                .accentColor(.red) // pick your own color
        }
    }
}

1
我认为这是正确的答案。Itay 上面的答案可能有效,但它并没有真正利用 SwiftUI。 - Justin Ngan

5
SceneDelegate.swift 中创建应用程序窗口时,可以使用 UIWindowtintColor 属性全局设置色调颜色。
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)
        }
    }
}

然后在您的视图中,您将像这样使用它:

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

我不使用SceneDelegate文件,因为我使用新的SwiftUI生命周期应用程序(它包含一个.app文件,这是应用程序的主文件)。 - Ariel Turjeman
你尝试在场景中的视图上设置.accentColor(.red)了吗? - Itay Brenner
它不像tintColor那样工作。它只会为连接的一个视图更改色调,而不是像tintColor一样更改整个应用程序的色调...感谢您的建议。 - Ariel Turjeman
它是从祖先继承而来的,但如果您有许多流可以使用环境变量 @Environment(\.accentColor) var accentColor - Itay Brenner
非常感谢您! - Ariel Turjeman
显示剩余3条评论

0
Ariel, 在这个实现中, 你可以选择并在整个应用程序生命周期中使用“accentColor”。但是,如果你想永久保留一个值, 你应该考虑一个解决方案, 我希望你是一个聪明的人...
 enum MyColor: Identifiable, CaseIterable {
    var id: String { UUID().uuidString }
    case blue, green, orange, pink, purple, red, yellow
    var currentColor: Color {
        switch self {
        case .blue:
            return .blue
        case .green:
            return .green
        case .orange:
            return .orange
        case .pink:
            return .pink
        case .purple:
            return .purple
        case .red:
            return .red
        case .yellow:
            return .yellow
        }
    }
}

final class ViewModel: ObservableObject {
    @Published
    var isPresented = false
    @Published
    var myColor: MyColor = .blue
}

struct AppSettings: View {
    @ObservedObject
    var vm: ViewModel
    var body: some View {
        NavigationView {
            Form {
                Picker("Current color", selection: $vm.myColor) {
                    ForEach(MyColor.allCases) { color in
                        Label(
                            title: {
                                Text(color.currentColor.description.capitalized)
                            }, icon: {
                                Image(systemName: "circle.fill")
                            })
                            .tag(color)
                            .foregroundColor(color.currentColor)
                    }
                }
            }
            .navigationBarItems(
                trailing:
                    Button(
                        action: {
                            vm.isPresented.toggle()},
                        label: {
                Text("Close")
            }))
            .navigationTitle("App settings")
        }
    }
}

struct ContentView: View {
    @StateObject
    private var vm = ViewModel()
    var body: some View {
        VStack {
            Button(
                action: {
                    vm.isPresented.toggle()
                }, label: {
                    VStack {
                        Rectangle()
                            .fill(Color.accentColor)
                            .frame(width: 100, height: 100)
                        Text("Settings")
                            .font(.title)
                    }
                })
        }
        .accentColor(vm.myColor.currentColor)
        .sheet(
            isPresented: $vm.isPresented) {
            AppSettings(vm: vm)
                .accentColor(vm.myColor.currentColor)
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接