简体   繁体   中英

push view after 2 seconds in swiftui

how to push a view after x seconds in swiftUI without any interaction?

 NavigationView{
            .onAppear {
                DispatchQueue.main.asyncAfter(deadline: .now()+1.0) {
                    NavigationLink(destination: pageViewUi()) {
                        EmptyView()
                    }
                }
            }
}

You can handle it it this way :


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()
                    }
            }
        }
    }
}

You need to manage it using a state variable:

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
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM