也许它不完全适合回答这个问题,在 iOS 7 及更高版本中,您可以通过这种方式自定义颜色:
在委托方法中
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
添加关注
[[pickerView.subviews objectAtIndex:1] setBackgroundColor:NEEDED_COLOR];
[[pickerView.subviews objectAtIndex:2] setBackgroundColor:NEEDED_COLOR];
更新
以前的代码有效,但马马虎虎。这里是 UIPickerView 的简单子类
斯威夫特:
class RDPickerView: UIPickerView
{
@IBInspectable var selectorColor: UIColor? = nil
override func didAddSubview(subview: UIView) {
super.didAddSubview(subview)
if let color = selectorColor
{
if subview.bounds.height <= 1.0
{
subview.backgroundColor = color
}
}
}
}
目标-C:
@interface RDPickerView : UIPickerView
@property (strong, nonatomic) IBInspectable UIColor *selectorColor;
@end
@implementation RDPickerView
- (void)didAddSubview:(UIView *)subview
{
[subview didAddSubview:subview];
if (self.selectorColor)
{
if (subview.bounds.size.height <= 1.0)
{
subview.backgroundColor = self.selectorColor;
}
}
}
@end
您可以直接在故事板中设置选择器颜色
感谢 Ross Barbish -“在 2015 年 12 月 8 日发布的 iOS 9.2 和 XCode 7.2 中,此选择视图的高度为 0.666666666666667”。
更新:
修复了 iOS 10 的问题,效果不佳但有效。 :/
class RDPickerView: UIPickerView
{
@IBInspectable var selectorColor: UIColor? = nil
override func didAddSubview(_ subview: UIView) {
super.didAddSubview(subview)
guard let color = selectorColor else {
return
}
if subview.bounds.height <= 1.0
{
subview.backgroundColor = color
}
}
override func didMoveToWindow() {
super.didMoveToWindow()
guard let color = selectorColor else {
return
}
for subview in subviews {
if subview.bounds.height <= 1.0
{
subview.backgroundColor = color
}
}
}
}
感谢 Dmitry Klochkov,我会努力寻找更好的解决方案。