티스토리 뷰
반응형
탭뷰의 아이템 터치 시 탭 전환 말고 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
스유
반응형
'iOS SwiftUI' 카테고리의 다른 글
iOS SwiftUI PageControl 페이지 컨트롤 (0) | 2024.03.01 |
---|---|
iOS SwiftUI FlowLayout 컨텐츠 크기 만큼 아래로 배치 되는 레이아웃 (2) | 2024.02.01 |
댓글