TabView 不显示下一个 N SelectionValue,其中 SelectionValue == UInt

TabView doesn't show the next N SelectionValue where SelectionValue == UInt

下面的代码不显示 Text("Second View"),因为 private var selection: UInt.

struct TabMenu: View {
    @State private var selection: UInt = 0

    var body: some View {
        TabView(selection: $selection){
            Text("First View")
                .font(.title)
                .tabItem {
                    Image(systemName: "house")
                    Text("Main")
            }
            .tag(0)

            Text("Second View")
                .font(.title)
                .tabItem {
                    Image(systemName: "gear")
                    Text("Preferences")
            }
            .tag(1)
        }
    }
}

在 TabView 的源代码中,我发现了这个:

extension TabView where SelectionValue == Int {

    @available(watchOS, unavailable)
    public init(@ViewBuilder content: () -> Content)
}

如何在 SelectionValue == Uint 中创建自定义视图?

where SelectionValue == Uint

选择的类型必须与标签的类型相同(默认情况下两者都是 Int)。

这是一个解决方案。使用 Xcode 11.4 / iOS 13.4

测试
struct TabMenu: View {
    @State private var selection: UInt = 0

    var body: some View {
        TabView(selection: $selection){
            Text("First View")
                .font(.title)
                .tabItem {
                    Image(systemName: "house")
                    Text("Main")
            }
            .tag(UInt(0))

            Text("Second View")
                .font(.title)
                .tabItem {
                    Image(systemName: "gear")
                    Text("Preferences")
            }
            .tag(UInt(1))
        }
    }
}