如何更改UIPickerView选择器的颜色

7

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


如果有人在寻找,我在这里为SwiftUI中的Picker添加了一个答案:https://dev59.com/U73pa4cB1Zd3GeqPm-Dv#69944302 SwiftUI在幕后使用UIPickerView,并访问其子视图,对我来说,这行代码很有效:picker.subviews[1].backgroundColor = UIColor.red - multitudes
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的简单子类。

Swift:

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
            }
        }

    }
}

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

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

感谢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,我会尝试找到更好的解决方案。


这对我来说是有效的,但是如果苹果对pickerView进行任何更改,就不能保证以后不会出现问题。 - Sid
谢谢!这是改变选择指示器颜色的唯一真正方法。在Swift中有效。 - ObjectiveTC
1
在iOS 9.2中,我必须将subview.bounds.size.height设置为<= 1.0,以便选择指示器条改变颜色。 - Nathan B.
应用程序因访问UIPickerView的子视图并直接修改它们而被拒绝吗? - Fengson
@Fengson 这里没有使用私有API,因此不会在苹果审核过程中出现问题。 - vsilux
显示剩余2条评论

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
        }
    }
}

4

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

无论如何,你可以将UIPickerView实例的showsSelectionIndicator设置为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元素需要更多的工作,而且这也必须正常工作。


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(记住,它是一个框架,所以它将根据父视图的坐标系进行定位)

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

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")
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接