Skip to content

Commit a7afcac

Browse files
committed
[fix] Preserve the state of each tab view when switching between tabs
1 parent 8548cf0 commit a7afcac

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

Example/Example/ContentView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ struct ContentView: View {
2929
Text("Home")
3030
.font(.system(.footnote, design: .rounded).weight(item == 0 ? .bold : .medium))
3131
}
32-
Text("Marks View")
32+
List(1...30, id: \.self) { Text("Row \($0)") }
3333
.tabItem(1) {
3434
Image(systemName: item == 1 ? "star.fill" : "star")
3535
.font(.title3)

Sources/TabBarModule/Internal/ViewModifier/TabItemViewModifier.swift

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,19 @@ struct TabItemViewModifier<Selection: Hashable, V: View>: ViewModifier {
2121
}
2222

2323
func body(content: Content) -> some View {
24-
Group {
25-
if selectionHashValue == item.hashValue {
26-
content
27-
} else {
28-
Color.clear
29-
}
30-
}
31-
.preference(key: ItemsPreferenceKey.self, value: [item])
32-
.preference(
33-
key: ItemViewBuilderPreferenceKey.self,
34-
value: [
35-
item: AnyItemViewBuilder(
36-
selectedItemHashValue: selectionHashValue,
37-
item: item,
38-
content: { AnyView(VStack(spacing: 0, content: itemBuilder)) }
39-
)
40-
]
41-
)
24+
content
25+
.opacity(selectionHashValue == item.hashValue ? 1 : 0)
26+
.disabled(!(selectionHashValue == item.hashValue))
27+
.preference(key: ItemsPreferenceKey.self, value: [item])
28+
.preference(
29+
key: ItemViewBuilderPreferenceKey.self,
30+
value: [
31+
item: AnyItemViewBuilder(
32+
selectedItemHashValue: selectionHashValue,
33+
item: item,
34+
content: { AnyView(VStack(spacing: 0, content: itemBuilder)) }
35+
)
36+
]
37+
)
4238
}
4339
}

0 commit comments

Comments
 (0)