Ios 更改UIPicker颜色
如何更改选择器的颜色 我不想改变字体或文字。我的选择器是填充式的,工作方式完全符合我的要求,我要做的是将颜色从黑色更改为自定义白色。请看: 如果要更改每个元素的标题颜色,可以实现:Ios 更改UIPicker颜色,ios,swift,colors,uipickerview,Ios,Swift,Colors,Uipickerview,如何更改选择器的颜色 我不想改变字体或文字。我的选择器是填充式的,工作方式完全符合我的要求,我要做的是将颜色从黑色更改为自定义白色。请看: 如果要更改每个元素的标题颜色,可以实现: func pickerView(pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? { let titleData = pickerDa
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
}
如果希望更多控制自定义选择器中的每个元素
使用“viewForRow”方法
显然,您返回的视图可能比UILabel更复杂。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
}
在UIPickerView中具有多个组件的Swift 4.0 每个“组件”都是您设计的UIPickerView中显示的列表。例如,如果您具有以下内容:数组中的姓名列表和数组中的年龄列表,并且这两个列表都会显示,以便您选择一个人的姓名和年龄。 forComponent中的第一个组件“名称”为component==0; forComponent中的第二个组件“age”是component==1; 依此类推,取决于您列出的组件数量
func pickerView(_ pickerView: UIPickerView, attributedTitleForRow row: Int, forComponent component: Int) -> NSAttributedString? {
if component == 0 {
let nameData = namesArray[row]
let myNameData = NSAttributedString(string: nameData, attributes: [.foregroundColor:UIColor.white])
return myNameData
} else {
let ageData = ageArray[row]
let myAgeData = NSAttributedString(string: ageData, attributes: [.foregroundColor:UIColor.white])
return myAgeData
}
}
*适用于日期选择器和普通选择器
override func viewDidLoad() {
super.viewDidLoad()
pickerView.setValue(UIColor.blue, forKey: "textColor")
pickerView.setValue(UIColor.black, forKey: "backgroundColor")
}
更改内容的颜色:字体、背景、选定项目、未选定项目?这正是我所需要的,并使我能够在黑色背景上的选择器视图中使用白色文本。感谢you@YouriNooijen,太好了,谢谢。
override func viewDidLoad() {
super.viewDidLoad()
pickerView.setValue(UIColor.blue, forKey: "textColor")
pickerView.setValue(UIColor.black, forKey: "backgroundColor")
}