A頁面——>B頁面——>C頁面——>D頁面,然后:D頁面——>B頁面。
-
效果如下:
代碼如下:
import SwiftUI
struct ContentViewA: View {
@State var isNavPush = false
var body: some View {
VStack {
HStack {
Spacer()
Text("ContentViewA")
.font(.system(size: 17,weight: .semibold))
Spacer()
}.frame(height: 44)
Spacer()
Button {
isNavPush = true
} label: {
Text("導(dǎo)航跳轉(zhuǎn)")
}
Spacer()
}.fullScreenCover(isPresented: $isNavPush, content: {
ContentViewB()
})
.padding()
}
}
struct ContentViewB: View {
@Environment(\.presentationMode) var presentationMode
// @Binding var rootIsActive : Bool
@State var isNavPush = false
var body: some View {
NavigationView {
VStack {
NavigationLink(isActive: $isNavPush) {
ContentViewC(rootIsActive: $isNavPush)
} label: {
}
Button {
isNavPush = true
} label: {
Text("導(dǎo)航跳轉(zhuǎn)")
}
}.navigationBarTitle("ContentViewB", displayMode: .inline) //設(shè)置標(biāo)題displayMode,默認(rèn)的是:automatic(大標(biāo)題)
.navigationBarBackButtonHidden(true) //隱藏系統(tǒng)的導(dǎo)航返回按鈕
.navigationBarItems(leading: Button(action: { //自定義導(dǎo)航的返回按鈕
presentationMode.wrappedValue.dismiss() //返回上級頁面
}, label: {
Image("nav_back_black") //導(dǎo)航返回按鈕圖標(biāo)
}))
.padding()
}
}
}
struct ContentViewC: View {
@Environment(\.presentationMode) var presentationMode
@Binding var rootIsActive : Bool
@State var isNavPush = false
var body: some View {
VStack {
NavigationLink(isActive: $isNavPush) {
ContentViewD(rootIsActive: $rootIsActive)
} label: {
}
Button {
isNavPush = true
} label: {
Text("導(dǎo)航跳轉(zhuǎn)")
}
}.navigationBarTitle("ContentViewC", displayMode: .inline) //設(shè)置標(biāo)題displayMode狞膘,默認(rèn)的是:automatic(大標(biāo)題)
.navigationBarBackButtonHidden(true) //隱藏系統(tǒng)的導(dǎo)航返回按鈕
.navigationBarItems(leading: Button(action: { //自定義導(dǎo)航的返回按鈕
presentationMode.wrappedValue.dismiss() //返回上級頁面
}, label: {
Image("nav_back_black") //導(dǎo)航返回按鈕圖標(biāo)
}))
.padding()
}
}
struct ContentViewD: View {
@Environment(\.presentationMode) var presentationMode
@Binding var rootIsActive : Bool
var body: some View {
VStack {
Button {
self.rootIsActive = false
} label: {
Text("返回根目錄")
}
}.navigationBarTitle("ContentViewD", displayMode: .inline) //設(shè)置標(biāo)題displayMode乏盐,默認(rèn)的是:automatic(大標(biāo)題)
.navigationBarBackButtonHidden(true) //隱藏系統(tǒng)的導(dǎo)航返回按鈕
.navigationBarItems(leading: Button(action: { //自定義導(dǎo)航的返回按鈕
presentationMode.wrappedValue.dismiss() //返回上級頁面
}, label: {
Image("nav_back_black") //導(dǎo)航返回按鈕圖標(biāo)
}))
.padding()
}
}
A頁面——>B頁面——>C頁面——>D頁面,然后:D頁面——>A頁面淋硝。
-
效果如下:
代碼如下:
import SwiftUI
struct ContentViewA: View {
@State var isNavPush = false
var body: some View {
NavigationView {
VStack {
NavigationLink(isActive: $isNavPush) {
ContentViewB(rootIsActive: $isNavPush)
} label: {
}
Button {
isNavPush = true
} label: {
Text("導(dǎo)航跳轉(zhuǎn)")
}
}.navigationBarTitle("ContentViewA", displayMode: .inline) //設(shè)置標(biāo)題displayMode雹熬,默認(rèn)的是:automatic(大標(biāo)題)
.padding()
}
}
}
struct ContentViewB: View {
@Environment(\.presentationMode) var presentationMode
@Binding var rootIsActive : Bool
@State var isNavPush = false
var body: some View {
VStack {
NavigationLink(isActive: $isNavPush) {
ContentViewC(rootIsActive: $rootIsActive)
} label: {
}
Button {
isNavPush = true
} label: {
Text("導(dǎo)航跳轉(zhuǎn)")
}
}.navigationBarTitle("ContentViewB", displayMode: .inline) //設(shè)置標(biāo)題displayMode,默認(rèn)的是:automatic(大標(biāo)題)
.navigationBarBackButtonHidden(true) //隱藏系統(tǒng)的導(dǎo)航返回按鈕
.navigationBarItems(leading: Button(action: { //自定義導(dǎo)航的返回按鈕
presentationMode.wrappedValue.dismiss() //返回上級頁面
}, label: {
Image("nav_back_black") //導(dǎo)航返回按鈕圖標(biāo)
}))
.padding()
}
}
struct ContentViewC: View {
@Environment(\.presentationMode) var presentationMode
@Binding var rootIsActive : Bool
@State var isNavPush = false
var body: some View {
VStack {
NavigationLink(isActive: $isNavPush) {
ContentViewD(rootIsActive: $rootIsActive)
} label: {
}
Button {
isNavPush = true
} label: {
Text("導(dǎo)航跳轉(zhuǎn)")
}
}.navigationBarTitle("ContentViewC", displayMode: .inline) //設(shè)置標(biāo)題displayMode谣膳,默認(rèn)的是:automatic(大標(biāo)題)
.navigationBarBackButtonHidden(true) //隱藏系統(tǒng)的導(dǎo)航返回按鈕
.navigationBarItems(leading: Button(action: { //自定義導(dǎo)航的返回按鈕
presentationMode.wrappedValue.dismiss() //返回上級頁面
}, label: {
Image("nav_back_black") //導(dǎo)航返回按鈕圖標(biāo)
}))
.padding()
}
}
struct ContentViewD: View {
@Environment(\.presentationMode) var presentationMode
@Binding var rootIsActive : Bool
var body: some View {
VStack {
Button {
self.rootIsActive = false
} label: {
Text("返回根目錄")
}
}.navigationBarTitle("ContentViewD", displayMode: .inline) //設(shè)置標(biāo)題displayMode竿报,默認(rèn)的是:automatic(大標(biāo)題)
.navigationBarBackButtonHidden(true) //隱藏系統(tǒng)的導(dǎo)航返回按鈕
.navigationBarItems(leading: Button(action: { //自定義導(dǎo)航的返回按鈕
presentationMode.wrappedValue.dismiss() //返回上級頁面
}, label: {
Image("nav_back_black") //導(dǎo)航返回按鈕圖標(biāo)
}))
.padding()
}
}
總結(jié):
1、使用導(dǎo)航跳轉(zhuǎn)继谚,返回到指定頁面時烈菌,目前還只知道這種方式(實際:都是返回到導(dǎo)航根頁面),目前還在SwiftUI的練習(xí)階段,如果有更好的處理方式芽世,還請不吝分享侨嘀。
2、上文中第二種效果中捂襟,如果實現(xiàn)D頁面——>B頁面也是可以實現(xiàn)的咬腕,需要在B頁面的VStack{}外套一層NavigationView{},然后rootIsActive的標(biāo)記從B頁面開始(而非A頁面開始)葬荷,但是會有兩個問題:a涨共、跳轉(zhuǎn)到的頁面會有兩層導(dǎo)航顯示。b宠漩、從D頁面直接返回到B頁面后举反,此時點擊B頁面的“導(dǎo)航跳轉(zhuǎn)”,則直接跳轉(zhuǎn)到了D頁面(正常情況下應(yīng)該跳轉(zhuǎn)C頁面)扒吁。
如果有完美解決以上問題的大神火鼻,還請分享分享,謝謝雕崩!~