Код: Выделить всё
// it need to be run on real device, not on simulator,
// because changing scheme reloads all view and pops
// all views from the stack. This is not the case.
@Observable
class Colors {
func textColor(for scheme: ColorScheme) -> Color {
scheme == .dark ? .white : .red
}
func backgroundColor(for scheme: ColorScheme) -> Color {
scheme == .dark ? .brown : .green
}
}
struct TestStartView: View {
private var colors = Colors()
@State private var path = NavigationPath()
@State var selection = 9
@State var index = 0
var body: some View {
TabView(selection: $index) {
SchemeTestView()
.environment(colors)
.tag(0)
}
.ignoresSafeArea(.container)
.tabViewStyle(.page(indexDisplayMode: .never))
// here is the problem ❌
}
}
struct SchemeTestView: View {
@Environment(Colors.self) var colors
@Environment(\.colorScheme) var scheme
@State var path = NavigationPath()
var body: some View {
NavigationStack(path: $path) {
InsideView()
.navigationDestination(for: String.self) { value in
InsideView()
}
}
}
}
struct InsideView: View {
@Environment(Colors.self) var colors
@Environment(\.colorScheme) var scheme
var body: some View {
ZStack {
colors.backgroundColor(for: scheme)
.ignoresSafeArea()
VStack {
Text("hello scheme")
.foregroundStyle(colors.textColor(for: scheme))
NavigationLink(value: "abc") {
Text("Tap me")
}
}
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... en-colorsc