s.d.
s.d.

Reputation: 363

How to set background color of UIPickerView using UIAppearance?

I am using the following line of code to change the background color of UIPickerView throughout my app as it is for a theme in the app. This changes nothing.

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

I also tried the following line but that give a weird shaded background that looks terrible.

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

Here is an image of what the above line shows: enter image description here

I want the background color to be one solid color.

The question is, how can I change the background color of UIPickerView in the whole app using UIAppearance?

Upvotes: 1

Views: 4764

Answers (3)

Mendy
Mendy

Reputation: 186

Swift 3.0:

I managed to solve it using the method func numberOfComponents (in pickerView: UIPickerView) -> Int like this:

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

Upvotes: 5

Devang Tandel
Devang Tandel

Reputation: 3008

As per my Knowledge UIPicker does not conforms to UI_APPEARANCE_SELECTOR for UIAppereance ..

[pickerView setBackgroundColor:[UIColor blueColor]];

OR You Might want use..

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

Above two are the solutions i would prefer to change Picker Appearance..There although an other way to set an image, but i won't suggest to use that.

Its up to you NOW..

Upvotes: 0

Abhishek Sharma
Abhishek Sharma

Reputation: 475

I have tried the code Please have a look,

 self.pickerView.backgroundColor=[UIColor grayColor];

This code is working for me fine.

In case It does not work for you, I'll recommend another way to change the background color.

Drag a UIView and have a the same size as UIPickerView is having, also select color of UIView color (the color that you want as backgound). Drag and Drop you picker view inside that View.

Please let me know, It's working or not.

Upvotes: 0

Related Questions