1

The code below doesn't show Text("Second View"), because 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)
        }
    }
}

In sources of TabView, I found this:

extension TabView where SelectionValue == Int {

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

How I can create a custom view where SelectionValue == Uint ?

where SelectionValue == Uint
Nick Rossik
  • 1,048
  • 1
  • 6
  • 16

1 Answers1

1

Selection must be the same type as tag is (by default both are Int).

Here is a solution. Tested with 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))
        }
    }
}
Asperi
  • 228,894
  • 20
  • 464
  • 690