SwiftUI导航链接屏幕在更改颜色主题时弹出。

huangapple go评论142阅读模式
英文:

SwiftUI navigation link screen pops when change color theme

问题

以下是您要翻译的代码部分:

  1. struct MyTabView: View {
  2. @Environment(\.colorScheme) var colorScheme
  3. @State var toggle = false
  4. var body: some View {
  5. TabView {
  6. ContentView(toggle: $toggle)
  7. .tabItem {
  8. Text("Profile")
  9. .foregroundColor(colorScheme == .dark ? .white : .black)
  10. }
  11. }
  12. .accentColor(colorScheme == .dark ? .white : .black)
  13. .navigationBarTitleDisplayMode(.inline)
  14. }
  15. }
  16. struct ContentView: View {
  17. @Binding var toggle: Bool
  18. var body: some View {
  19. NavigationLink(destination: Text("madmasdmsadas"), isActive: $toggle) {
  20. Text("Click")
  21. }
  22. }
  23. }
  24. @main
  25. struct AppTesting: App {
  26. var body: some Scene {
  27. WindowGroup {
  28. NavigationView {
  29. MyTabView()
  30. }
  31. }
  32. }
  33. }

请注意,代码中的特殊字符已被正确翻译。

英文:

I have a TabBar view with a single tab (for the demo purposes). The tab view screen has a NavigationLink button which sends you to a screen with a Text box only. The problem that occurs is that whenever I am on the second screen and change the color theme one time - nothing special happens, theme changes and all. When I change it a second time afterwards, the screen just dismisses itself without a reason. Anyone has any idea why this might be happenning?

  1. struct MyTabView: View {
  2. @Environment(\.colorScheme) var colorScheme
  3. @State var toggle = false
  4. var body: some View {
  5. TabView {
  6. ContentView(toggle: $toggle)
  7. .tabItem {
  8. Text("Profile")
  9. .foregroundColor(colorScheme == .dark ? .white : .black)
  10. }
  11. }
  12. .accentColor(colorScheme == .dark ? .white : .black)
  13. .navigationBarTitleDisplayMode(.inline)
  14. }
  15. }
  16. struct ContentView: View {
  17. @Binding var toggle: Bool
  18. var body: some View {
  19. NavigationLink(destination: Text("madmasdmsadas"), isActive: $toggle) {
  20. Text("Click")
  21. }
  22. }
  23. }
  24. @main
  25. struct AppTesting: App {
  26. var body: some Scene {
  27. WindowGroup {
  28. NavigationView {
  29. MyTabView()
  30. }
  31. }
  32. }
  33. }

I tried adding binding values to keep it active but the problem doesn't get resolved.

答案1

得分: 2

理想情况下,NavigationView 应该位于 TabView 之下,而不是相反的情况。

至于为什么会发生这种情况:我对此没有清晰的想法。可能是一个导致 toggle 状态重置的 bug。但相应地更新了代码对我有所帮助。

更新后的代码:

  1. import SwiftUI
  2. struct MyTabView: View {
  3. @Environment(\.colorScheme) var colorScheme
  4. @State var toggle = false
  5. var body: some View {
  6. TabView {
  7. NavigationView {
  8. ContentView(toggle: $toggle)
  9. }
  10. .tabItem {
  11. Text("Profile")
  12. .foregroundColor(colorScheme == .dark ? .white : .black)
  13. }
  14. }
  15. .accentColor(colorScheme == .dark ? .white : .black)
  16. .navigationBarTitleDisplayMode(.inline)
  17. }
  18. }
  19. struct ContentView: View {
  20. @Binding var toggle: Bool
  21. var body: some View {
  22. NavigationLink(destination: Text("madmasdmsadas"), isActive: $toggle) {
  23. Text("Click")
  24. }
  25. }
  26. }
  27. @main
  28. struct AppTesting: App {
  29. var body: some Scene {
  30. WindowGroup {
  31. MyTabView()
  32. }
  33. }
  34. }
英文:

Ideally, the NavigationView should be under TabView. Not vice versa.

As for why this is happening: I don't have a clear thought about this. Perhaps a bug that causes the toggle state to reset. But updating the code accordingly has helped me.

The updated code:

  1. import SwiftUI
  2. struct MyTabView: View {
  3. @Environment(\.colorScheme) var colorScheme
  4. @State var toggle = false
  5. var body: some View {
  6. TabView {
  7. NavigationView {
  8. ContentView(toggle: $toggle)
  9. }
  10. .tabItem {
  11. Text("Profile")
  12. .foregroundColor(colorScheme == .dark ? .white : .black)
  13. }
  14. }
  15. .accentColor(colorScheme == .dark ? .white : .black)
  16. .navigationBarTitleDisplayMode(.inline)
  17. }
  18. }
  19. struct ContentView: View {
  20. @Binding var toggle: Bool
  21. var body: some View {
  22. NavigationLink(destination: Text("madmasdmsadas"), isActive: $toggle) {
  23. Text("Click")
  24. }
  25. }
  26. }
  27. @main
  28. struct AppTesting: App {
  29. var body: some Scene {
  30. WindowGroup {
  31. MyTabView()
  32. }
  33. }
  34. }

huangapple
  • 本文由 发表于 2023年2月14日 19:07:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75446939.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定