如何更改UIPickerView选择器的颜色

kir*_*iri 5 uipickerview uikit ios

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

vsi*_*lux 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
Run Code Online (Sandbox Code Playgroud)

添加以下内容

[[pickerView.subviews objectAtIndex:1] setBackgroundColor:NEEDED_COLOR];
[[pickerView.subviews objectAtIndex:2] setBackgroundColor:NEEDED_COLOR];
Run Code Online (Sandbox Code Playgroud)

UPDATE

以前的代码有效,但一般.这里是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
            }
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

Objective-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
Run Code Online (Sandbox Code Playgroud)

您可以直接在故事板中设置选择器颜色

感谢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
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

谢谢Dmitry Klochkov,我会尽力找到更好的解决方案.


Iva*_*van 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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Joo*_*ost 4

我想你正在处理iPhone SDK?可能还有一些其他框架使用这个名称,所以也许您可以添加标签以包括 uikit、cocoa-touch 等。

无论如何,您可以将showsSelectionIndicatorUIPickerView 实例设置为 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];
Run Code Online (Sandbox Code Playgroud)

破解 UI 元素本身需要做更多的工作,而且这也必须有效。