标签栏用大小写代替int - SwiftUI



我试图重用一些旧代码从我以前问过的问题,它不再工作了,是不是发生了什么事?

当我午餐的应用程序TabBar去索引0而不是2。这是我当前的代码:

struct iOS_TabBarView: View {

@State private var selectedTab: HostingBarCategories = .Screen3

var body: some View {
TabView(selection: $selectedTab) {
Text("1")
.tag(0)
.tabItem {
Image(systemName: "pencil.and.outline")
Text("1")
}
Text("2")
.tag(1)
.tabItem {
Image(systemName: "checkmark")
Text("2")
}
Text("3")
.tag(2)
.tabItem {
Image(systemName: "calendar.circle.fill")
Text("3")
}
Text("4")
.tag(3)
.tabItem {
Image(systemName: "flame")
Text("4")
}
Text("5")
.tag(3)
.tabItem {
Image(systemName: "slider.horizontal.3")
Text("5")
}
}
}
}

和HostingBarCategories:

enum HostingBarCategories: Hashable, CaseIterable {
case Screen1
case Screen2
case Screen3
case Screen4
case Screen5

var string: String { String(describing: self) }
}

怎么回事?

我假设标签&选择应该是相同的类型,所以尝试

struct iOS_TabBarView: View {

@State private var selectedTab: HostingBarCategories = .Screen3

var body: some View {
TabView(selection: $selectedTab) {
Text("1")
.tag(HostingBarCategories.Screen1)    // << like this for all !!

最新更新