Skip to content

Commit 053bfdd

Browse files
graycreateclaude
andauthored
feat: Implement scroll-to-top for native TabView (#54)
* feat: implement scroll-to-top for native TabView using Combine Implement proper SwiftUI approach for detecting tab reselection: - Add Combine PassthroughSubject to capture all tab taps - Create intermediate binding that publishes tap events - Use onReceive to dispatch TabbarClickAction for scroll-to-top - Works for both tab switches and same-tab taps - Pure SwiftUI solution without UIKit introspection This enables the native iOS behavior where tapping an active tab scrolls content to the top, matching standard iOS app behavior. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> * fix: update tab selection unconditionally for consistency Apply Copilot suggestion to always update selectedTab binding value, even when tapping the same tab. This ensures the TabView selection stays in sync and simplifies the binding logic. Setting the same value is harmless in SwiftUI and prevents potential sync issues between the intermediate binding and the actual state. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <[email protected]> --------- Co-authored-by: Claude <[email protected]>
1 parent 20050d7 commit 053bfdd

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

V2er/View/MainPage.swift

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
//
88

99
import SwiftUI
10+
import Combine
1011

1112
struct MainPage: StateView {
1213
@EnvironmentObject private var store: Store
14+
@State private var tabReselectionPublisher = PassthroughSubject<TabId, Never>()
1315

1416
var bindingState: Binding<GlobalState> {
1517
$store.appState.globalState
@@ -22,10 +24,26 @@ struct MainPage: StateView {
2224
store.appState.feedState.feedInfo.unReadNums
2325
}
2426

27+
// Create an intermediate binding that captures all tab selections
28+
// This is the proper SwiftUI way to detect same-tab taps without UIKit
29+
private var tabSelection: Binding<TabId> {
30+
Binding(
31+
get: { selectedTab.wrappedValue },
32+
set: { newValue in
33+
// Publish the tap event before changing the value
34+
// This allows us to detect same-tab taps
35+
tabReselectionPublisher.send(newValue)
36+
37+
// Always update the selection to maintain consistency
38+
selectedTab.wrappedValue = newValue
39+
}
40+
)
41+
}
42+
2543
var body: some View {
2644
NavigationView {
2745
ZStack {
28-
TabView(selection: selectedTab) {
46+
TabView(selection: tabSelection) {
2947
// Feed Tab
3048
pageWithTopBar(
3149
FeedPage(selecedTab: state.selectedTab)
@@ -83,6 +101,10 @@ struct MainPage: StateView {
83101
.zIndex(1000)
84102
}
85103
}
104+
.onReceive(tabReselectionPublisher) { tappedTab in
105+
// Dispatch action for all tab taps (including same-tab taps)
106+
dispatch(TabbarClickAction(selectedTab: tappedTab))
107+
}
86108
.navigationBarHidden(true)
87109
}
88110
}

0 commit comments

Comments
 (0)