I created a grid with some buttons, each button is associated to a certain color and changes the view's background color:
@IBOutlet weak var backgroundSelectionView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
backgroundSelectionView.isHidden = true
backgroundSelectionView.isUserInteractionEnabled = false
backgroundGrid()
}
@IBAction func backgroundColor(_ sender: Any) {
if backgroundSelectionView.isHidden == true {
backgroundSelectionView.isHidden = false
backgroundSelectionView.isUserInteractionEnabled = true
} else {
backgroundSelectionView.isHidden = true
backgroundSelectionView.isUserInteractionEnabled = false
}
}
func backgroundGrid(){
blackButtonOutlet.layer.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
blackButtonOutlet.layer.borderWidth = 1
blackButtonOutlet.layer.borderColor = UIColor.black.cgColor
}
@IBOutlet weak var blackButtonOutlet: UIButton!
@IBAction func blackButton(_ sender: Any) {
view.layer.backgroundColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
}
Right now it works fine, if I press the button it sets the background and everything looks nice. The only problem is that when I navigate in the app or leave it, the background becomes white(default) again. I'd like to set it with userDefaults, how do I do it?