方法1:NavigationView
import SwiftUI
struct ContentView: View {
var body: some View {
NavigationView {
List(0..<100) { row in
NavigationLink(destination: Text("Detail \(row)")) {
Text("Hello world")
}
}.navigationBarTitle("SwiftUI")
}
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1503ad740d9894f005785fcbde3f1db1.png)
方法二:sheet
import SwiftUI
struct TestView: View {
@State var isClicked = false
var body: some View {
Button("ClickMe") {
isClicked = true
}.sheet(isPresented: $isClicked, content: {
AnotherView()
})
}
}
struct AnotherView:View {
var body: some View {
Text("AnotherView")
}
}
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/fc78f9584b36ba6ccd76643dd9300cfa.png)
方法三:
import SwiftUI
struct TestView: View {
@State var isShowing = false
var body: some View {
if isShowing {
AnotherView().animation(.spring()).transition(.scale)
} else {
Button("ClickMe") {
self.isShowing = true
}
}
}
}
struct AnotherView:View {
var body: some View {
Text("AnotherView")
}
}
方法四:跳转且可返回
import SwiftUI
struct TestView: View {
@State var isShowing = false
var body: some View {
Button("Button"){
isShowing.toggle()
}.fullScreenCover(isPresented: $isShowing, content: {
AnotherView.init()
})
}
}
struct AnotherView:View {
@Environment(\.presentationMode) var mode
var body: some View {
Button("ClickMe") {
self.mode.wrappedValue.dismiss()
}.font(.title)
}
}