I want the Swift UI to display in the Navigation View at specified times like the Starbucks app ("good morning" at 5:00, "good afternoon" at 12:00, "good night" at 17:00). please tell me.
Asked
Active
Viewed 80 times
1 Answers
0
You can compare current time with the time you have set for each title to achieve what you're looking for
import SwiftUI
struct SwiftUIView: View {
// MARK: - PROPERTIES
@State private var navigationTitle = ""
// MARK: - BODY
var body: some View {
NavigationView {
VStack{
}//: VSTACK
.navigationTitle(navigationTitle) //TITLE
}//: NAVIGATION
.onAppear {
let morningTime = "05:00:00"
let noonTime = "12:00:00"
let nightTime = "17:00:00"
//DateFormatter
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "hh:mm:ss"
let currentTime = dateFormatter.string(from: Date())
//Conditions to compare time
if currentTime >= morningTime && currentTime < noonTime{
navigationTitle = "Good Morning"
}else if currentTime >= noonTime && currentTime < nightTime{
navigationTitle = "Good Afternoon"
}else{
navigationTitle = "Good Night"
}
}
}
}
// MARK: - PREVIEW
struct SwiftUIView_Previews: PreviewProvider {
static var previews: some View {
SwiftUIView()
}
}

Umair Khan
- 993
- 8
- 14