Hey is that possible to change background image in an app using hour and minutes? For example sunrise at 7:30 and it will change background to a sunrise picture and time goes by it will change into morning at specific Hour and minutes too. If it can how to setup in swift. Thank you
-
What background? Something in your app or the home screen? What is your specific question? Right now this is much too broad. Please [edit] your question (no comments) with more specific details about your issue. – rmaddy Nov 27 '18 at 21:46
-
1Believe it or not, the concepts of OOP are still in play even in iOS and even with Swift. So treat this like any other engineering problem, step by logical step. The first place I may start might be with a Google search for something like "swift call function at specific time"... and go from there. – trndjc Nov 27 '18 at 21:50
-
what background? is not clear question please edit your question – Hichem Romdhane Nov 27 '18 at 23:20
-
Hey @rmaddy a background of an app I just change my question – ferryawijayanto Nov 28 '18 at 02:40
3 Answers
If you're talking about changing the iOS background on the HomeScreen, no. Swift does not give you that much capability over iOS. However, if you are talking about changing the background in your app to reflect the time of day, then it is absolutely possible. The Best way to go about it would be to check the time as soon as your app is opened using a function in your AppDelegate.swift file called
applicationDidBecomeActive
Once there, you need to capture the date and set it to a global variable (see here for more information regarding how to do this if you're unfamiliar)
after you have set the Date to a global variable, you will need to tell your application to change the background while the app is loading your view. This is just pseudocode of what the code will actually look like, but I hope it helps:
let sunRiseTime : Date = //insert date you would like the background to change here.
let noon : Date = //insert date for noon here
let sunriseBackgroundImage : UIImage = //insert image you would like for the sunrise here
let noonBackgroundImage : UIImage = //insert image you would like for noon here.
if(dateGlobalVariable >= sunriseTime){
self.backgroundImage = sunriseBackgroundImage
}else if(dateGlobalVariable >= noon{
self.backgroundImage = noonBackgroundImage
}
//continue this for as many images as you have.
hope this helps! let me know if you have any questions!

- 214
- 2
- 14
-
FYI - the inability to change the home screen background has nothing to do with Swift. You can't do it in Objective-C either. – rmaddy Nov 28 '18 at 00:33
-
This code won't change the background while the user is using the app. It only works if they leave and come back. – rmaddy Nov 28 '18 at 00:34
-
@cdunn2013 thank you for your reply, I'm so frustrated to change background image in my app with the right time. I only can do it with Hour using timer, but that's not what I want until you give me a good solution. Thanks again!! I will ask you again if I have some trouble ☺️ – ferryawijayanto Nov 28 '18 at 02:38
-
@rmaddy correct, I forgot to mention that it would be good practice to put the same function you put in your AppDelegate in your ViewDidLoad to pass the global variable. – cdunn2013 Nov 28 '18 at 02:39
-
@ferryawijayanto sounds good, let me know if you need any more help and I will help you one on one. – cdunn2013 Nov 28 '18 at 02:42
-
@cdunn2013 I want to ask, is the global variable I should store the current date or from 24:00/00:00. I still have a hard time to set the global variable though, please help me hehe – ferryawijayanto Nov 28 '18 at 09:22
You can do it like that:
func setupTimer(hour: Int, minute: Int, second: Int, newColor: UIColor){
let calendar = Calendar.current.date(bySettingHour: hour, minute: minute, second: second, of: Date())
let timer = Timer(fireAt: calendar!, interval: 0, target: self, selector: #selector(changeBackground(_:)), userInfo: UIColor.gray, repeats: true)
RunLoop.main.add(timer, forMode: .common)
}
@objc func changeBackground(_ timer: Timer?){
if let newColor = timer?.userInfo as? UIColor {
DispatchQueue.main.async {
self.view.backgroundColor = newColor
}
}
}
-
This is the easy part. You've left out the hard part which is knowing what to pass to `setupTimer`. – rmaddy Nov 28 '18 at 00:36
-
@rmaddy Yes but this question doesn’t contain so much details and I assumed that author of the question only doesn’t know how to change something in background and skip previous parts beacsuse as you mansion is the hard part and requires few steps including implementing CoreLocation, API request, JSON parsing... – Janek Kruczkowski Nov 28 '18 at 10:06
Below method in AppDelgate will do the job. func applicationWillEnterForeground(_ application: UIApplication) { // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. }
This will get called when the application is opened/launched. change the app's background according to the time.

- 225
- 2
- 13
-
`change the app's background according to the time` That's exactly what OP is asking, and you're not answering. – Eric Aya Nov 28 '18 at 13:03