1

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?

jscs
  • 63,694
  • 13
  • 151
  • 195
Mommy
  • 93
  • 12
  • 2
    [Saving UIColor to and loading from NSUserDefaults](https://stackoverflow.com/questions/1275662/saving-uicolor-to-and-loading-from-nsuserdefaults)... anyway: your logic in `IBAction` can by simplified to: `@IBAction func backgroundColor(_ sender: Any) { backgroundSelectionView.isHidden.toggle() backgroundSelectionView.isUserInteractionEnabled.toggle() }` – Robert Dresler Mar 14 '19 at 20:48
  • Also you don’t have to set `isUserInteractionEnabled` at all, since if view is hidden, user can’t interact with it – Robert Dresler Mar 14 '19 at 22:40

1 Answers1

2

I don't see the other buttons and where you change the background color but let's assume you are using an HeX represantation of a color ypu could save the background color in shared preferences and always use the shared state to set the background.

When you want to save the color to user prefs: UserDefaults.standard.set("#FF2200", forKey: "background_color_hex")

To retrieve the HEX represantation of background color from prefs: let hexBackgroundColor = UserDefaults.standard.string(forKey: “background_color_hex”) ?? “#FFFFFF”.

Then to use it you can have an extension to get a UIColor based on its hex represantation like explained in many articles online, take for example this one https://www.hackingwithswift.com/example-code/uicolor/how-to-convert-a-hex-color-to-a-uicolor like this: view.backgroundColor = UIColor(hex: hexBackgroundColor)

There can be a lot of other solutions I guess, this one is the quickest that came to mind.

denis_lor
  • 6,212
  • 4
  • 31
  • 55
  • I am using color literal and I change the background when I press blackButton – Mommy Mar 14 '19 at 21:00
  • I don't see the reason for that as it makes managing colors and themes in your app and other stacks you might have (android, ios, web) in future more complicated in my opinion. But whatever good reason that I am not aware you have you can save either your 4 values of red, green, blue, alpha in shared prefs and retrieve them. Or can save them as one string concatenating them and then extract it from the string when retrieving the string from prefs. `store some value in user default`, `retrieve it`, `use it`. – denis_lor Mar 14 '19 at 21:05
  • You are always setting the background color using literals to `#colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)` instead of having different values of red, green, blue, alpha. Whenever you change the background color, set the different values and then store them in User Defaults. Then in your `viewDidLoad` you can retrieve the values from User Defaults and set the background with those value retrieven from `User Defaults` – denis_lor Mar 14 '19 at 21:15