7

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

4

5 回答 5

23

也许它不完全适合回答这个问题,在 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 -“随着 iOS 9.2 和 XCode 7.2 于 2015 年 12 月 8 日发布,此选择视图的高度为 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,我会尝试找到一些更好的解决方案。

于 2014-11-10T19:58:58.940 回答
11

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

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

斯威夫特 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
        }
    }
}
于 2016-01-26T09:57:16.907 回答
4

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

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

// 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 元素本身需要更多的工作,这也必须工作。

于 2009-09-23T14:40:14.993 回答
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
于 2015-07-01T10:11:46.137 回答
0

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

import UIKit

class CustomPickerView: UIPickerView {
    
    init() {
        super.init(frame: .zero)
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        subviews.forEach { $0.backgroundColor = .clear }
    }
    
    override init(frame: CGRect) {
        fatalError("init(coder:) has not been implemented")
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
于 2020-10-16T07:44:18.920 回答