要求:
当ConversionViewController
显示时,根据当前时间来更新背景颜色。可在晚上更新背景颜色为深色,白天更新为亮色。
Hint:可以重载viewWillAppear(:)
来实现功能(可以在每次显示时都改变背景颜色)
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)// 根据上下午更改背景颜色let dateformatter = DateFormatter()dateformatter.dateFormat = "aaa"print(dateformatter.string(from: Date()))if dateformatter.string(from: Date()) == dateformatter.amSymbol {
view.backgroundColor = UIColor.white}else {
view.backgroundColor = UIColor.black}// 根据早晚更改背景颜色let formatter = DateFormatter()formatter.dateFormat = "HH"let str = formatter.string(from: Date())let time = Int8(str)if (time! >= 18 || time! <= 6) {
view.backgroundColor = UIColor.darkGray}else{
view.backgroundColor = UIColor.gray}// 每次显示随机更改颜色let color = CGFloat(CGFloat(arc4random())/CGFloat(RAND_MAX))let color1 = CGFloat(CGFloat(arc4random())/CGFloat(RAND_MAX))let color2 = CGFloat(CGFloat(arc4random())/CGFloat(RAND_MAX))let color3 = CGFloat(CGFloat(arc4random())/CGFloat(RAND_MAX))self.view.backgroundColor = UIColor.init(red: color, green: color1, blue: color2, alpha: color3)}
参考链接:
iOS-更改状态栏背景和文字颜色(OC + Swift3)
ios开发判断时间是白天还是晚上
Swift如何判断上午还是下午
swift 随机生成背景颜色