如何在swiftUI中在x秒后推送视图而没有任何交互?
NavigationView{
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now()+1.0) {
NavigationLink(destination: pageViewUi()) {
EmptyView()
}
}
}
}
答案 0 :(得分:7)
您可以这样处理:
struct ContentView: View {
@State var show = false
var body: some View {
NavigationView{
NavigationLink(destination: Text("OK"), isActive: $show, label: {
EmptyView()
})
.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
self.show.toggle()
}
}
}
}
}
答案 1 :(得分:0)
您需要使用状态变量进行管理:
import SwiftUI
struct Home : View {
@State var isActive = false
var body: some View {
NavigationView {
VStack {
Text("Home")
NavigationLink(destination: Details(), isActive: self.$isActive) {
EmptyView()
}
}
}.onAppear {
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
self.isActive.toggle()
}
}
}
}
struct Details : View {
var body: some View {
VStack {
Text("sample code")
}.navigationBarBackButtonHidden(true) // To hide back button
}
}