如何解决在使用ForEach的SwiftUI中选择时,是否可以更改tabItem的图像?
考虑SwiftUI中最简单的选项卡视图应用程序:
struct ContentView: View {
@State private var selection = 0
var body: some View {
TabView(selection: $selection){
Text("First View")
.font(.title)
.tabItem {
VStack {
Image("first")
Text("First")
}
}
.tag(0)
Text("Second View")
.font(.title)
.tabItem {
VStack {
Image("second")
Text("Second")
}
}
.tag(1)
}
}
}
每当点击选项卡时,都会重新加载选项卡项目,并且如果需要,可以通过将选择内容与标签进行比较来更改所选选项卡的图像。但是,如果变量中的标签数量是动态的,并且使用ForEach
来显示它们,则该方法将无效:
struct ContentView: View {
@State private var selection = 0
private var items: [AnyView] {
return [
AnyView(Text("First View")
.font(.title)
.tabItem {
VStack {
Image("first1")
Text("First1")
}
}
.tag(0)),AnyView(Text("Second View")
.font(.title)
.tabItem {
VStack {
Image("second")
Text("Second")
}
}
.tag(1))
]
}
var body: some View {
TabView(selection: $selection){
ForEach(0..<self.items.count) { index in
self.items[index]
}
}
}
}
重新加载视图时,不会调用ForEach
的正文。是否可以在TabView中使用ForEach
的同时完成图像更改?
解决方法
尝试以下操作:
struct ContentView: View {
@State private var selection = 0
var body: some View {
TabView(selection: $selection){
Text("First View")
.font(.title)
.tabItem {
VStack {
Image(systemName: selection == 0 ? "xmark" : "plus")
Text("First")
}
}
.tag(0)
Text("Second View")
.font(.title)
.tabItem {
VStack {
Image(systemName: selection == 1 ? "xmark" : "minus")
Text("Second")
}
}
.tag(1)
}
}
}
,
答案在文档中!在范围内使用ForEach时:
/// Creates an instance that computes views on demand over a *constant*
/// range.
///
/// This instance only reads the initial value of `data` and so it does not
/// need to identify views across updates.
///
/// To compute views on demand over a dynamic range use
/// `ForEach(_:id:content:)`.
因此,请使用ForEach(_:id:content:)
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。