【问题标题】:SwiftUI ColorPicker cause pop to root viewSwiftUI ColorPicker 导致弹出到根视图
【发布时间】:2022-10-15 23:25:16
【问题描述】:

我有这个奇怪的问题。当我单击 ColorPicker 时,视图消失并弹回根视图。我在下面附上了屏幕记录。任何人都知道如何解决这个问题?

代码在模拟器 ios 15.2 上运行

See screen record here

import SwiftUI

struct ContentView: View {
    var body: some View {
        NavigationView {
            TabView {
                HomePageView()
                    .tabItem {
                        Label("Home", systemImage: "circle.grid.cross.fill")
                    }
              
                ProfileView()
                    .tabItem {
                        Label("Profile", systemImage: "person.fill")
                    }
            }
            .background(.secondary)
        }
    }
}

struct HomePageView: View {
    var body: some View {
        Text("Home View")
    }
}

struct ProfileView: View {
    var body: some View {
        NavigationLink {
            ColorPickerView()
        } label: {
            Text("Color Picker View")
        }
    }
}

struct ColorPickerView: View {
    @State var bgColor = Color.blue
    var body: some View {
        ColorPicker("Pick color", selection: self.$bgColor)
            .padding(.horizontal, 15)
    }
}

【问题讨论】:

  • 将 NavigationView 放在每个选项卡中,而不是放在选项卡视图之外
  • 对我来说效果很好,在 macos 12.3-beta 上,使用 xcode 13.3-beta,针对 ios 15 和 macCatalyst 12。您可以尝试将:.navigationViewStyle(.stack) 添加到NavigationView 或使用@lorem ipsum 建议。
  • TabViews 是将NavigationView 放在顶层的规则的一个例外。它必须进入选项卡项目视图。
  • 感谢你们。 @loremipsum 的解决方案有效。

标签: swiftui swiftui-navigationview swiftui-tabview


【解决方案1】:

NavigationView 在每个选项卡中,而不是在 TabView 之外

【讨论】:

  • 嗨@loremipsum,所以在我将 NavigationView 放入每个选项卡项之后,底部的选项卡栏总是出现在每个详细视图中。你知道怎么隐藏吗?
  • @yang1818 有一些解决方法可以像您的原始问题和 cmets 中的其他解决方案一样隐藏,但这种行为违背了苹果 guidelines 据我所知,隐藏标签栏没有完美的解决方案。你总是放弃一些东西。
【解决方案2】:

新建议:在 iOS16 中使用 NavigationStack 来克服弹出到上一个或有时弹出到根视图的问题。

【讨论】:

    猜你喜欢
    • 2021-12-29
    • 1970-01-01
    • 2022-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多