Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change UIPicker color

How can I change the color of my picker?

I don't wish to change the font or the text. My picker is populate and works exactly how I want it, what I want to do is change the color from black to my custom white.

like image 627
user4671001 Avatar asked Mar 24 '15 21:03

user4671001


3 Answers

Take a look at: http://makeapppie.com/tag/uipickerview-in-swift/

If you want to change your title color of each element you could implement:

func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
    let titleData = pickerData[row]
    var myTitle = NSAttributedString(string: titleData, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 15.0)!,NSForegroundColorAttributeName:UIColor.whiteColor()])
    return myTitle
}

Swift 3:

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
    let titleData = pickerData[row]
    let myTitle = NSAttributedString(string: titleData!, attributes: [NSFontAttributeName:UIFont(name: "Georgia", size: 15.0)!,NSForegroundColorAttributeName:UIColor.white])
    return myTitle
}
like image 99
Youri Nooijen Avatar answered Nov 03 '22 04:11

Youri Nooijen


*For both Date picker and Normal Picker

   override func viewDidLoad() {
   super.viewDidLoad()
   pickerView.setValue(UIColor.blue, forKey: "textColor")
   pickerView.setValue(UIColor.black, forKey: "backgroundColor")
 }
like image 27
Sai kumar Reddy Avatar answered Nov 03 '22 06:11

Sai kumar Reddy


Swift 4.0

func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
    let titleData = arrayOfPickerData[row]
    let myTitle = NSAttributedString(string: titleData, attributes: [.font:UIFont(name: "Georgia", size: 15.0)!, .foregroundColor:UIColor.white]))
    return myTitle
}
like image 31
makle Avatar answered Nov 03 '22 04:11

makle