如何使用 UIAppearance 设置 UIPickerView 的背景颜色?

How to set background color of UIPickerView using UIAppearance?

我正在使用以下代码行来更改整个应用程序中 UIPickerView 的背景颜色,就像应用程序中的主题一样。这没有任何改变。

 [[UIPickerView appearance] setBackgroundColor: [UIColor orangeColor]];

我也尝试了下面这行,但是它给出了一个看起来很糟糕的奇怪的阴影背景。

[[UIView appearanceWhenContainedInInstancesOfClasses:@[[UIPickerView class]]] setBackgroundColor: [UIColor darkGrayColor]];

这是上面一行显示的图像:

我希望背景颜色是一种纯色。

问题是,如何使用UIAppearance更改整个应用程序中UIPickerView的背景颜色?

代码我已经试过了,请看一下,

 self.pickerView.backgroundColor=[UIColor grayColor];

这段代码对我来说很好用。

如果它不适合你, 我会推荐另一种更改背景颜色的方法。

拖动一个 UIView 并使其具有与 UIPickerView 相同的大小, 还有 select UIView 颜色的颜色(你想要作为背景的颜色)。 将您的选择器视图拖放到该视图中。

请告诉我它是否有效。

据我所知,UIPicker 不符合 UI_APPEARANCE_SELECTOR 的 UIAppereance ..

[pickerView setBackgroundColor:[UIColor blueColor]];

或者您可能想要使用..

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

以上两个是我更喜欢更改选择器外观的解决方案..虽然还有其他设置图像的方法,但我不建议使用它。

现在由你决定..

Swift 3.0:

我设法使用方法 func numberOfComponents (in pickerView: UIPickerView) -> Int 解决了这个问题:

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    pickerView.backgroundColor = UIColor.blackDark
    return 1
}