UIPickerView 在视图中时不会更改颜色WillAppearif 语句



我正在制作一个应用程序,其中有4个选项卡,最后一个选项卡用于更改为"黑模式"(更改标签颜色,bg颜色,键盘颜色和pickerview颜色(。功能是我改变颜色的地方。除了Pickerview不会改变颜色,一切都起作用,所以这就是我需要的帮助。

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
        }
    }
}

您的代码段中的问题(_:attributedTitleForrow:forcomponent :)委派在 viewWillAppear中被声明为 nested函数范围,如下:

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
        }
    }

,请确保实现:

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

代码中的某个地方。

有关更多信息,您可能需要检查此答案。

最新更新