【问题标题】:change global textcolor of UIPickerView possible?可以更改 UIPickerView 的全局文本颜色吗?
【发布时间】:2017-06-04 20:45:49
【问题描述】:

我的两个pickerViews 有问题。我想知道是否可以在 AppDelegate 文件中更改我的 pickerViews 的全局文本颜色?

我知道有些事情可以这样做:

UIPickerView.appearance().backgroundColor = color6
UIPickerView.appearance().tintColor = color4

如果我需要在每个pickerView 中手动将其写入我的代码中。应该把它编码成什么样的pickerView,titleForRow?

【问题讨论】:

  • 您可以只为UIPickerView 编写扩展或子类,并在init 函数中设置色调颜色。这样可以防止您编写重复的代码。
  • 您好,Linus,感谢您的评论!好吧,这是否意味着我应该为颜色创建一个类并像这样添加 uipickerView ?类颜色:UIPickerView{ }
  • 没有。创建选择器视图的子类:class MyPickerView: UIPickerView {...}。然后,编写一个自定义的 init 函数并在其中设置默认的 tint 颜色。
  • @Adrian 请检查我的更新答案
  • @SahilManchanda 刚看到这个。我明天去看看。非常感谢。

标签: swift3 uipickerview


【解决方案1】:

尝试使用以下类。我对 UIPickerView 进行了子类化,并在其中指定了颜色。这工作正常。当您设置项目时。它将委托和数据源设置为 self 并为您完成工作。

   import UIKit
    class CustomUIPickerView: UIPickerView{
        let textColor: UIColor = .white
        let backGroundColor: UIColor = .blue
        let _tintColor: UIColor = .white
        var items: [String]?{
            didSet{
                self.delegate = self
                self.dataSource = self
                self.reloadAllComponents()
            }
        }
        override init(frame: CGRect) {
            super.init(frame: frame)
           commonInit()
        }

        func commonInit(){
            self.backgroundColor = backGroundColor
            self.tintColor = _tintColor
        }

        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            commonInit()
        }
    }
    extension CustomUIPickerView: UIPickerViewDataSource{
        func numberOfComponents(in pickerView: UIPickerView) -> Int {
            return 1
        }

        func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
            return self.items?.count ?? 0
        }

    }
    extension CustomUIPickerView: UIPickerViewDelegate{
        func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
            let string = self.items?[row] ?? ""
            return NSAttributedString(string: string, attributes: [NSAttributedStringKey.foregroundColor: textColor])
        }
    }

但是,如果您想在视图控制器中处理委托和数据源,那么您可以利用 Extensions 的优势,方法如下:

extension String{
    func stringForPickerView() -> NSAttributedString{
        let color: UIColor = .white
        return NSAttributedString(string: self, attributes: [NSAttributedStringKey.foregroundColor: color])
    }
}

然后在你的 ViewController 中:

extension ViewController: UIPickerViewDelegate{
    func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
        return self.items[row].stringForPickerView() // from Extension
    }
}

【讨论】:

  • 华丽!它工作得很好。我创建的自定义类做得还不够。非常感谢!它还没有让我接受它,但我会在赏金到期时接受。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多