Ios UIPickerView没有';t在视图中更改颜色将显示if语句
我正在制作一个应用程序,其中我有4个选项卡,最后一个选项卡用于更改为暗模式(更改标签颜色、背景颜色、键盘颜色和pickerView颜色)。func是我改变颜色的地方。除了pickerView没有改变颜色,其他一切都正常,这就是我需要帮助的地方Ios UIPickerView没有';t在视图中更改颜色将显示if语句,ios,arrays,swift,uipickerview,viewwillappear,Ios,Arrays,Swift,Uipickerview,Viewwillappear,我正在制作一个应用程序,其中我有4个选项卡,最后一个选项卡用于更改为暗模式(更改标签颜色、背景颜色、键盘颜色和pickerView颜色)。func是我改变颜色的地方。除了pickerView没有改变颜色,其他一切都正常,这就是我需要帮助的地方 override func viewWillAppear(_ animated: Bool) { if UserDefaults.standard.integer(forKey: "dark") == 1{ self.view.
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
}
}
}
代码段中的问题是,委托在
视图中声明为嵌套函数时将出现,如果其作用域如下所示,则应将其清除:
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
在代码的某个地方
有关详细信息,您可能需要查看。欢迎使用SO。关于你的问题,你能更具体一点吗?你会遇到什么错误,什么不起作用?picker视图颜色更改现在起作用,但只有在我开始使用pickerView时才起作用。viewWillApear中func的原因是我需要它在我使用黑暗模式swift(黑暗)时立即切换,就像所有其他标签和按钮一样。