UIPickerView 在 viewWillAppear if 语句中不会改变颜色
Posted
技术标签:
【中文标题】UIPickerView 在 viewWillAppear if 语句中不会改变颜色【英文标题】:UIPickerView doesn't change color when in viewWillAppear if statement 【发布时间】:2017-06-28 12:47:12 【问题描述】:我正在制作一个应用程序,其中有 4 个选项卡,最后一个选项卡用于更改为暗模式(更改标签颜色、背景颜色、键盘颜色和 pickerView 颜色)。 func 是我改变颜色的地方。一切正常,除了 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
【问题讨论】:
欢迎来到 SO。你能更具体地谈谈你的问题吗?你得到什么错误,什么不起作用? 【参考方案1】:你的代码sn-p中pickerView(_:attributedTitleForRow:forComponent:)委托被声明为viewWillAppear
中的嵌套函数的问题,如果它的作用域应该超出,如下:
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
在你的代码中。
有关更多信息,您可能需要查看this answer。
【讨论】:
选择器视图颜色更改现在有效,但仅当我开始使用选择器视图时。 viewWillApear 中的函数的原因是因为我需要它在我使用与所有其他标签和按钮相同的暗模式 swift (dark) 时立即切换。以上是关于UIPickerView 在 viewWillAppear if 语句中不会改变颜色的主要内容,如果未能解决你的问题,请参考以下文章
即使在 [[UIPickerView alloc] init] 之后设置了委托,也没有调用 UIPickerView 委托方法