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

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

SwiftUI navigation link screen pops when change color theme

问题

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

struct MyTabView: View {
    @Environment(\.colorScheme) var colorScheme
    @State var toggle = false
    
    var body: some View {
        TabView {
            ContentView(toggle: $toggle)
                .tabItem {
                    Text("Profile")
                        .foregroundColor(colorScheme == .dark ? .white : .black)
                }
        }
        .accentColor(colorScheme == .dark ? .white : .black)
        .navigationBarTitleDisplayMode(.inline)
    }
}

struct ContentView: View {
    @Binding var toggle: Bool
    
    var body: some View {
        NavigationLink(destination: Text("madmasdmsadas"), isActive: $toggle) {
            Text("Click")
        }
    }
}

@main
struct AppTesting: App {
    var body: some Scene {
        WindowGroup {
            NavigationView {
                MyTabView()
            }
        }
    }
}

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

英文:

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?

struct MyTabView: View {
    @Environment(\.colorScheme) var colorScheme
    @State var toggle = false
    
    var body: some View {
        TabView {
            ContentView(toggle: $toggle)
                .tabItem {
                    Text("Profile")
                        .foregroundColor(colorScheme == .dark ? .white : .black)
                }
        }
        .accentColor(colorScheme == .dark ? .white : .black)
        .navigationBarTitleDisplayMode(.inline)
    }
}

struct ContentView: View {
    @Binding var toggle: Bool
    
    var body: some View {
        NavigationLink(destination: Text("madmasdmsadas"), isActive: $toggle) {
            Text("Click")
        }
    }
}

@main
struct AppTesting: App {
    var body: some Scene {
        WindowGroup {
            NavigationView {
                MyTabView()
            }
        }
    }
}

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

答案1

得分: 2

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

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

更新后的代码:

import SwiftUI

struct MyTabView: View {
  @Environment(\.colorScheme) var colorScheme
  @State var toggle = false
  
  var body: some View {
    TabView {
      NavigationView {
        ContentView(toggle: $toggle)
      }
      .tabItem {
        Text("Profile")
          .foregroundColor(colorScheme == .dark ? .white : .black)
      }
    }
    .accentColor(colorScheme == .dark ? .white : .black)
    .navigationBarTitleDisplayMode(.inline)
  }
}

struct ContentView: View {
  @Binding var toggle: Bool
  
  var body: some View {
    NavigationLink(destination: Text("madmasdmsadas"), isActive: $toggle) {
      Text("Click")
    }
  }
}

@main
struct AppTesting: App {
  var body: some Scene {
    WindowGroup {
      MyTabView()
    }
  }
}

英文:

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:

import SwiftUI

struct MyTabView: View {
  @Environment(\.colorScheme) var colorScheme
  @State var toggle = false
  
  var body: some View {
    TabView {
      NavigationView {
        ContentView(toggle: $toggle)
      }
      .tabItem {
        Text("Profile")
          .foregroundColor(colorScheme == .dark ? .white : .black)
      }
    }
    .accentColor(colorScheme == .dark ? .white : .black)
    .navigationBarTitleDisplayMode(.inline)
  }
}

struct ContentView: View {
  @Binding var toggle: Bool
  
  var body: some View {
    NavigationLink(destination: Text("madmasdmsadas"), isActive: $toggle) {
      Text("Click")
    }
  }
}

@main
struct AppTesting: App {
  var body: some Scene {
    WindowGroup {
      MyTabView()
    }
  }
}

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:

确定