menuView必須具有每個人的默認顏色。但是,使用RGB滑塊進行應用程序設置的任何人都可以更改menuView的顏色。 所以我想用黃色設置默認顏色,並設置滑塊來改變顏色menuView。換色後,可以保存。使用默認顏色啓動應用程序
import UIKit
class ViewController: UIViewController {
let defaults = UserDefaults.standard
@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var menuView: UIViewX!
var redval = Float()
var greenval = Float()
var blueval = Float()
@IBOutlet weak var NewMessage: UIButton!
@IBOutlet weak var Camera: UIButton!
@IBOutlet weak var NewPost: UIButton!
var tableData: [Model] = []
override func viewDidLoad() {
super.viewDidLoad()
self.menuView.backgroundColor = UIColor(red: CGFloat(redval), green: CGFloat(greenval), blue: CGFloat(blueval), alpha: 0.45)
}
}
以字符串
self.menuView.backgroundColor = UIColor(red: CGFloat(redval), green:
CGFloat(greenval), blue: CGFloat(blueval), alpha: 0.45)
我可以通過滑塊RGB與應用改變顏色,但現在我想先關稅默認的顏色,這可能是變化的。
代碼滑塊變色
import UIKit
class ViewSettings: UIViewController {
@IBOutlet var redslider: UISlider!
@IBOutlet var greenslider: UISlider!
@IBOutlet var blueslider: UISlider!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func change(_ sender: AnyObject) {
self.performSegue(withIdentifier: "ChangeColor", sender: self)
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "ChangeColor" {
let destanitaion = segue.destination as! ViewController
destanitaion.redval = redslider.value
destanitaion.blueval = blueslider.value
destanitaion.greenval = greenslider.value
}
}
}
直截了當地做到這一點是每一個紅,綠,藍值保存爲'UserDefaults'中的'Double'。然後提取每個組件並重建「UIColor」。你也可以創建一個'UserDefaults'擴展名來使這個更清潔。 – Paolo
你能舉個例子嗎?我想要的默認顏色是#colorLiteral(紅色:0.9966391622,綠色:1,藍色:0.2095825737,alpha:0.7946946448)/ *#f2f0f0 */@Paolo –
UP @Paolo查看更新 –