티스토리 뷰

반응형

탭뷰의 아이템 터치 시 탭 전환 말고 Navigation 이동

 

 

/// iOS 16 이상 버전
struct ContentView: View {
    
    @State private var isDetailViewPresented = false
    @State private var selectedTabIndex = 0
    
    var body: some View {
        
        NavigationStack {
            TabView(selection: $selectedTabIndex) {
                
                Text("Tab 0 Content")
                    .tabItem {
                        Image(systemName: "0.circle.fill")
                        Text("Tab 0")
                    }
                    .tag(0)
                Text("Tab 1 Content")
                    .tabItem {
                        Image(systemName: "1.circle.fill")
                        Text("Tab 1")
                    }
                    .tag(1)
                Text("Tab 2 Content")
                    .tabItem {
                        Image(systemName: "2.circle.fill")
                        Text("Tab 2")
                    }
                    .tag(2)
            }
            .onChange(of: selectedTabIndex) { oldValue, newValue in
                if newValue == 2 {
                    self.selectedTabIndex = oldValue
                    isDetailViewPresented = true
                }
            }
            .navigationDestination(isPresented: $isDetailViewPresented) {
                Text("navigationDestination")
            }
        }
    }
}

 

 

/// iOS 16 미만 버전
struct ContentView2: View {
    
    @State private var isDetailViewPresented = false
    @State private var selectedTabIndex = 0
    
    var body: some View {
        
        NavigationView {
            TabView(selection: $selectedTabIndex) {
                
                Text("Tab 0 Content")
                    .tabItem {
                        Image(systemName: "0.circle.fill")
                        Text("Tab 0")
                    }
                    .tag(0)
                Text("Tab 1 Content")
                    .tabItem {
                        Image(systemName: "1.circle.fill")
                        Text("Tab 1")
                    }
                    .tag(1)
                Text("Tab 2 Content")
                    .tabItem {
                        Image(systemName: "2.circle.fill")
                        Text("Tab 2")
                    }
                    .tag(2)
            }
            .onChange(of: selectedTabIndex) { [selectedTabIndex] newTab in
                if newTab == 2 {
                    self.selectedTabIndex = selectedTabIndex
                    isDetailViewPresented = true
                }
            }
            .background(
                NavigationLink(
                    destination: Text("NavigationLink"),
                    isActive: $isDetailViewPresented
                ) {
                    EmptyView()
                }
                .hidden()
            )
        }
    }
}

 

iOS

Swift

Xcode

SwiftUI

스유

반응형
댓글
300x250
반응형
최근에 올라온 글
최근에 달린 댓글
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
Total
Today
Yesterday