英文:
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()
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论