如何更改UIPickerView选择器的颜色

时间:2009-09-23 14:27:16

标签: ios uikit uipickerview

我有一个UIPicker,我想改变选择器的颜色。是否可以更改选择器的颜色?

5 个答案:

答案 0 :(得分:21)

也许它不完全适合回答这个问题,在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,我会尝试找到更好的解决方案。

答案 1 :(得分:11)

这是对vsilux答案的改进,以UIPickerView的简单类别的形式,而不需要子类化UIPickerView。

(截至2018年11月2日的最新答案)

Swift 4,Xcode 10:

@IBInspectable var selectorColor: UIColor? {
    get {
        return objc_getAssociatedObject(self, &selectorColorAssociationKey) as? UIColor
    }
    set(newValue) {
        objc_setAssociatedObject(self, &selectorColorAssociationKey, newValue,
                                 objc_AssociationPolicy.OBJC_ASSOCIATION_RETAIN)
    }
}

open override func didAddSubview(_ subview: UIView) {

    super.didAddSubview(subview)
    if let color = selectorColor {
        if subview.bounds.height < 1.0 {
            subview.backgroundColor = color
        }
    }
}

答案 2 :(得分:4)

我想你正在处理iPhone SDK?可能有一些其他框架使用此名称,因此您可以添加标签以包含uikit,cocoa-touch等。

无论如何,您可以将UIPickerView实例的showsSelectionIndicator设置为NO,因此它会隐藏选择器。然后,您可以使用调整后的选择样式创建一个新视图,并将其添加到UIPickerView上方的超级视图中。

// Some sample code, but you can do this in IB if you want to
_pickerView = [[UIPickerView alloc] init];
_pickerView.showsSelectionIndicator = NO;
[_pickerView sizeToFit];
[self.view addSubview:_pickerView];

UIImage *selectorImage = [UIImage imageNamed:@"selectorImage.png"]; // You have to make it strechable, probably
UIView *customSelector = [[UIImageView alloc] initWithImage:selectorImage];
customSelector.frame = CGRectZero; // Whatever rect to match the UIImagePicker
[self.view addSubview:customSelector];
[customSelector release];

攻击UI元素本身将需要更多的工作,而且这也必须起作用。

答案 3 :(得分:0)

您也可以只更改选择指标的背景颜色 只需在选择指示器上方添加一个UIView (它将成为您的叠加视图),将其设置为 alpha值低(取决于您,但我喜欢我的叠加层看起来透明) ,给它一个背景色,你很高兴。

考虑一下,

var overlayOnPicker:UIView = UIView(frame: CGRectMake(1, 68, mypicker.frame.width, 26))
// Adds a layer on the selection indicator

确实把 CGRect的X值= 1     (请记住,它是一个框架,因此它将根据superview的坐标系放置)

overlayOnPicker.backgroundColor = UIColor.whiteColor()
overlayOnPicker.alpha = 0.2
myDatePicker.addSubview(overlayOnPicker)
// You have to add the overlayOnPicker view as a subview to the Date Picker.
// myDatePicker is the UIDatePicker that I declared earlier in my code

答案 4 :(得分:0)

您还可以继承UIPickerView的子类并添加以下自定义项:

"runtime": {
  "type": "date",
  "format": "HH_mm_ss_SS"
},