0

I am making an app where I have 4 tabs, the last tab is used to change to dark mode (changing the label color, BG color, keyboard color and pickerView color). The func is where I change the color. Everything works except the pickerView isn't changing color so thats what I need help with.

override func viewWillAppear(_ animated: Bool) {

    if UserDefaults.standard.integer(forKey: "dark") == 1{

        self.view.backgroundColor = UIColor.darkGray
        UITextField.appearance().keyboardAppearance = .dark
        textField.backgroundColor = UIColor.white
        textField.textColor = UIColor.black
        textField2.textColor = UIColor.black
        textField2.backgroundColor = UIColor.white
        label.textColor = UIColor.white
        label.textColor = UIColor.white
        labelIN1.textColor = UIColor.white

        func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
            let titleData = list[row]
            let myTitle = NSAttributedString(string: titleData, attributes: [NSForegroundColorAttributeName:UIColor.black])
            return myTitle
        }


    }else{

        self.view.backgroundColor = UIColor.white
        UITextField.appearance().keyboardAppearance = .light
        textField.backgroundColor = UIColor.white
        textField.textColor = UIColor.black
        textField2.textColor = UIColor.black
        textField2.backgroundColor = UIColor.white
        label.textColor = UIColor.black
        label.textColor = UIColor.black
        labelIN1.textColor = UIColor.black

        func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
            let titleData = list[row]
            let myTitle = NSAttributedString(string: titleData, attributes: [NSForegroundColorAttributeName:UIColor.white])
            return myTitle
        }

    }

}
Ahmad F
  • 30,560
  • 17
  • 97
  • 143

1 Answers1

1

The problem in your code snippet that pickerView(_:attributedTitleForRow:forComponent:) delegate is declared as a nested function in viewWillAppear, it should be out if its scope, as follows:

override func viewWillAppear(_ animated: Bool) {

        if UserDefaults.standard.integer(forKey: "dark") == 1{

            self.view.backgroundColor = UIColor.darkGray
            UITextField.appearance().keyboardAppearance = .dark
            textField.backgroundColor = UIColor.white
            textField.textColor = UIColor.black
            textField2.textColor = UIColor.black
            textField2.backgroundColor = UIColor.white
            label.textColor = UIColor.white
            label.textColor = UIColor.white
            labelIN1.textColor = UIColor.white

        }else{

            self.view.backgroundColor = UIColor.white
            UITextField.appearance().keyboardAppearance = .light
            textField.backgroundColor = UIColor.white
            textField.textColor = UIColor.black
            textField2.textColor = UIColor.black
            textField2.backgroundColor = UIColor.white
            label.textColor = UIColor.black
            label.textColor = UIColor.black
            labelIN1.textColor = UIColor.black

        }
    }

    func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {

        let titleData = list[row]

        if UserDefaults.standard.integer(forKey: "dark") == 1{

            let myTitle = NSAttributedString(string: titleData, attributes: [NSForegroundColorAttributeName:UIColor.white])
            return myTitle

        } else {

            let myTitle = NSAttributedString(string: titleData, attributes: [NSForegroundColorAttributeName:UIColor.black])
            return myTitle
        }
    }

Also, make sure to implement:

  • class ViewController: UIViewController, UIPickerViewDelegate { /*...*/ }
  • pickerView.delegete = self

somewhere in your code.

For more information, you might want to check this answer.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
  • The picker view color changing works now but only when i start using the pickerView. The reason the func in viewWillApear was because i need it to instantly switch whenever i use the dark mode swift (dark) the same as all the other labels and buttons. – aleksabaskot Jun 29 '17 at 12:15