【问题标题】:How to change app tint color (New SwiftUI life cycle app)?如何更改应用程序色调颜色(新的 SwiftUI 生命周期应用程序)?
【发布时间】:2020-09-25 13:30:17
【问题描述】:

在 SwiftUI 应用中更改应用色调颜色的最佳方法是什么?

它由新的 SwiftUI 生命周期提供支持,因此我没有执行 self.?tintColor 的选项

另外,我该如何实现这个菜单?

(猜它使用了选择器和表单,但我无法在颜色名称旁边添加圆圈)

尝试在此处搜索,但未在 SwiftUI 生命周期应用中找到任何方法。

【问题讨论】:

  • 您应该问一个问题以更加专注,并为您的第二个问题创建一个新问题。

标签: swift swiftui


【解决方案1】:

在您为应用创建窗口的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 文件,它是应用程序的主文件)。
  • 您是否尝试将.accentColor(.red) 设置为场景中的视图?
  • 它不像 tintColor 那样工作。它只是改变它所连接的 1 个视图的色调,而不是像 tintColor 这样的整个应用程序......谢谢你的建议。
  • 它是从祖先那里继承来的,但是如果你有很多流,你可以使用 en environment var @Environment(\.accentColor) var accentColor
  • “accentcolor”由于“内部”保护级别而无法访问
【解决方案2】:

爱丽儿, 在这个实现中, 您可以在整个应用程序生命周期中选择和使用“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)
        }
    }
}

【讨论】:

    猜你喜欢
    • 2021-09-18
    • 1970-01-01
    • 1970-01-01
    • 2010-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-08-16
    • 1970-01-01
    相关资源
    最近更新 更多