【问题标题】:How to change the color of UIPickerView Selector如何更改 UIPickerView 选择器的颜色
【发布时间】:2009-09-23 14:27:15
【问题描述】:

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

【问题讨论】:

  • 如果有人在寻找它,我在这里为 SwiftUI 中的 Picker 添加了一个答案:stackoverflow.com/a/69944302/9497800 SwiftUI 在后台使用 UIPickerView 并访问它的子视图,这条线对我来说效果很好:@ 987654322@

标签: ios uikit uipickerview


【解决方案1】:

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

【讨论】:

  • 这确实对我有用,但如果 Apple 对 pickerView 进行任何更改,则不能保证将来不会中断。
  • 谢谢!这是更改选择指示器颜色的唯一真正方法。快速工作。
  • 在 iOS 9.2 中,我必须设置 subview.bounds.size.height
  • 在 iOS 10 上,这只适用于第一次显示包含选择器的视图。当视图第二次显示时,指示器颜色恢复为默认值。
  • @Dmitry Klochkov,我添加了修补程序。
【解决方案2】:

这是对 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
        }
    }
}

【讨论】:

    【解决方案3】:

    我想您正在处理 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 元素本身需要更多的工作,而且这也必须有效。

    【讨论】:

      【解决方案4】:

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

      考虑一下,

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

      并且确实把 CGRect 的 X 值=1 (记住,是一个frame,所以会按照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
      

      【讨论】:

        【解决方案5】:

        您还可以继承 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")
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-11-01
          • 1970-01-01
          相关资源
          最近更新 更多