SwiftUI\xe2\x80\x99s 选项卡选择应该适用于任何可哈希的内容,但这似乎不起作用。
\n在提供的示例中,您可以看到在 \xe2\x80\x9cWorking\xe2\x80\x9d Tab 中,如果您使用整数作为选项卡选择,则所有内容都可以正常工作。当您切换到 \xe2\x80\x9cBroken\xe2\x80\x9d 选项卡时,选择的是 ColorItem,并且选择不会更新视图。
\n我相信这是一个 SwiftUI 错误并已提交反馈(FB8879981)。
\n使用 Xcode 12.2 和 iOS 14.2(RC) 进行测试。
\nstruct ColorItem: Identifiable, Hashable{\n let color: Color\n let title: String\n \n var id: String{\n title\n }\n}\nRun Code Online (Sandbox Code Playgroud)\nstruct ContentView: View {\n let items = [\n ColorItem(color: .red, title: "Red"),\n ColorItem(color: .blue, title: "Blue"),\n ColorItem(color: .purple, title: "Purple")\n ]\n \n var body: some View {\n TabView{\n TabViewWorking(items: items)\n .tabItem {\n Label("Working", systemImage: "hand.thumbsup")\n }\n \n TabViewBroken(items: items)\n .tabItem {\n Label("Broken", systemImage: "hand.thumbsdown")\n }\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\nstruct TabViewWorking: View {\n @State private var tabSelection = 0\n let items: [ColorItem]\n \n var body: some View {\n ZStack{\n TabView(selection: $tabSelection){\n ForEach(0..<items.count){ i in\n items[i].color.edgesIgnoringSafeArea(.all)\n .tag(i)\n }\n }\n .tabViewStyle(PageTabViewStyle())\n \n VStack{\n Text(tabSelection.description)\n Text(items[tabSelection].title)\n }\n .font(.largeTitle)\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\nstruct TabViewBroken: View {\n @State private var tabSelection = ColorItem(color: .red, title: "Red")\n let items: [ColorItem]\n\n var body: some View {\n ZStack{\n TabView(selection: $tabSelection){\n ForEach(0..<items.count){ i in\n items[i].color.edgesIgnoringSafeArea(.all)\n .tag(i)\n }\n }\n .tabViewStyle(PageTabViewStyle())\n \n VStack{\n Text(items.firstIndex(of: tabSelection)?.description ?? "N/A")\n Text(tabSelection.title)\n }\n .font(.largeTitle)\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
Asp*_*eri 12
不,这不是 SwiftUI 错误。选择的类型和标签的类型必须相同,因此在第一个场景中它们都是整数,但在第二个场景中它们不同 - 选择是ColorItem,但标签仍然是整数 - 因此选择不起作用。
这是固定变体:
struct TabViewBroken: View {
@State private var tabSelection = ColorItem(color: .red, title: "Red")
let items: [ColorItem]
var body: some View {
ZStack{
TabView(selection: $tabSelection){
ForEach(0..<items.count){ i in
items[i].color.edgesIgnoringSafeArea(.all)
.tag(items[i]) // << here !!
}
}
.tabViewStyle(PageTabViewStyle())
VStack{
Text(items.firstIndex(of: tabSelection)?.description ?? "N/A")
Text(tabSelection.title)
}
.font(.largeTitle)
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4530 次 |
| 最近记录: |