I made a little example replicating what's going on, if you run it, and click on
set future date
, and wit 5 seconds, you'll see that the box hasn't changed color, after that, click on Go to view 2
and go back to view 1 and you'll see how the box color changes... that's what's happening in my code too:import SwiftUI
struct ContentView: View {
@State var past = Date()
@State var futuredate = Date()
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: DetailView())
{ Text("Go to view 2") }
Button("set future date") {
self.futuredate = self.past.addingTimeInterval(5)
}
VStack {
if (past < futuredate) {
Button(action: {
}) {
Text("")
}
.padding()
.background(Color.blue)
} else {
Button(action: {
}) {
Text("")
}
.padding()
.background(Color.black)
}
}
.onAppear {
self.past = Date()
}
}
}
}
}
struct DetailView: View {
@Environment(\.presentationMode) var presentationMode: Binding
var body: some View {
Text("View 2")
}
}
Any help would be appreciated
It may be very hard.
You may start a timer to check if `futuredate` has come or not every second.
struct ContentView: View {
@State var now = Date()
@State var futuredate = Date()
let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()
var body: some View {
NavigationView {
VStack {
NavigationLink(destination: DetailView()) {
Text("Go to view 2")
}
Button("set future date") {
self.futuredate = self.now.addingTimeInterval(5)
}
VStack {
if now < futuredate {
Button(action: {
}) {
Text("")
}
.padding()
.background(Color.blue)
} else {
Button(action: {
}) {
Text("")
}
.padding()
.background(Color.black)
}
}
}
}
.onReceive(timer) { _ in
self.now = Date()
}
}
}
But the timer might be stopped when got into background, so you might need to re-invoke it when in foreground again.