Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want to change UIView background color with time of day

Tags:

ios

swift

uiview

Pretty simple, I just want to change my backgroundView's color depending on the time of day, but for some reason it doesn't work. Any help would be great.

@IBOutlet weak var backgroundView: UIView!

override func viewDidLoad() {
    super.viewDidLoad()

    let darkColor=UIColor(red: 51, green: 52, blue: 50, alpha: 1.0)
    let greyColor=UIColor(red: 238, green: 240, blue: 239, alpha: 1.0)
    let hour = NSCalendar.current.component(.hour, from: NSDate() as Date)

    switch hour {
        case 1..<6: self.backgroundView.backgroundColor = darkColor
           break
        case 7..<18: self.backgroundView.backgroundColor = greyColor
           break
        case 19..<24: self.backgroundView.backgroundColor = darkColor
           break
    default:self.backgroundView.backgroundColor = greyColor
    }
}
like image 936
Otheprogrammer Avatar asked Mar 11 '23 07:03

Otheprogrammer


2 Answers

The problem is that the following lines are ignoring 6, 18, 24, respectively:

1..<6 //1 - 5

7..<18 //7 - 17

19..<24 //19 - 23

Depending on the time you had, when you checked it; if it happened to fall on either 6, 18, 24, then it could be the reason why you thought it is not working.

Using your code, I confirmed that the backgroundColor of the view does change.

Try changing the code to the following:

switch hour
{
     // hours 1 to 6
     case 1...6: self.backgroundView.backgroundColor = darkColor
            break
     // hours 7 to 18
     case 7...18: self.backgroundView.backgroundColor = greyColor
            break
     //hours 19 to 23 and 0
     case 19...23, 0: self.backgroundView.backgroundColor = darkColor
            break
     default:self.backgroundView.backgroundColor = greyColor
}

Another problem is the following:

let darkColor=UIColor(red: 51, green: 52, blue: 50, alpha: 1.0)

let greyColor=UIColor(red: 238, green: 240, blue: 239, alpha: 1.0)

as they should be:

let darkColor = UIColor(red: 51/255, green: 52/255, blue: 50/255, alpha: 1.0)

let greyColor = UIColor(red: 238/255, green: 240/255, blue: 239/255, alpha: 1.0)
like image 106
Unheilig Avatar answered Mar 12 '23 20:03

Unheilig


While running your app, you may have gotten a console log message in Xcode that says something like:

[Graphics] UIColor created with component values far outside the expected range. Set a breakpoint on UIColorBreakForOutOfRangeColorComponents to debug. This message will only be logged once.

The UIColor init(red:green:blue:alpha:) initializer expects a value between 0.0 and 1.0. In order to translate a RGB value that is within 0-255 to a value between 0.0 to 1.0, you need to divide your desired color intensity by 255.

Try changing your color declarations from:

let darkColor = UIColor(red: 51, green: 52, blue: 50, alpha: 1.0)

let greyColor = UIColor(red: 238, green: 240, blue: 239, alpha: 1.0)

to

let darkColor = UIColor(red: 51/255.0, green: 52/255.0, blue: 50/255.0, alpha: 1.0)

let greyColor = UIColor(red: 238/255.0, green: 240/255.0, blue: 239/255.0, alpha: 1.0)

EDIT:

@Unheilig answer makes a good point about your switch logic for the hour. However if this is intentional, then your default case would catch hours 0, 6, 18 and 24 and your background color would be greyColor.

like image 24
Ryan H. Avatar answered Mar 12 '23 19:03

Ryan H.