16

How to set the background color of UIPickerView on iOS 7 using SDK 7 and use a standard picker on iOS 5 and 6? It's transparent by default on iOS 7.

Charles
  • 50,943
  • 13
  • 104
  • 142
Dmitry
  • 14,306
  • 23
  • 105
  • 189
  • I would use - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view with custom view with background color – Yanchi Oct 01 '13 at 16:59
  • But it will work for all versions of iOS. I need only for iOS 7 (corrected on the question). – Dmitry Oct 01 '13 at 17:04

6 Answers6

39

What's wrong with:

[picker setBackgroundColor:[UIColor whiteColor]];

I'm assuming you have a reference to the picker view if you're invoking it, and it's a subclass of UIView so backgroundColor is a valid property...

Jesse
  • 10,370
  • 10
  • 62
  • 81
  • 2
    sometimes it can be so simple – Carmen Oct 23 '13 at 12:39
  • 1
    This doesn't work for me in iOS 7. The background of the picker continues to inherit/blend color from the view behind it. – Clifton Labrum Mar 07 '14 at 23:28
  • Hmm, just spun up a fresh project with nothing but a UIViewController with a picker in it. Set the view & picker's background colors to black, then set the pickerview to white in viewDidLoad, it worked as expect. Things to try:1. Set the color to red instead of white, it's easier to verify that it's actually setting the color at all. 2. set a breakpoint where you set the color, make sure it's being called. 3. Make sure you're not setting it anywhere else. – Jesse Mar 08 '14 at 00:25
  • I use picker.backgroundColor = [UIColor whiteColor]; but yeah the same thing. – Seamus Apr 01 '14 at 17:25
  • Its true. Straight forward :) – Augustine P A Jun 23 '16 at 06:42
9

I wanted to write it as a comment, but it would be hard to read :) Sooo....

-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 50, 44)]; // your frame, so picker gets "colored"
    label.backgroundColor = [UIColor lightGrayColor];
    label.textColor = [UIColor blackColor];
    label.font = [UIFont fontWithName:@"HelveticaNeue-Bold" size:18];
    label.text = [NSString stringWithFormat:@"%d",row];

    return label;
}

Also it doesnt have to be only label, I think you can insert other subviews there as well... It works on iOS7 as far as I know pic here

Yanchi
  • 1,030
  • 1
  • 20
  • 31
  • I have tried the provided code and it doesn't work. I don't know why yet. `picker.delegate = self;` is used. – Dmitry Oct 01 '13 at 17:07
  • Where does it fail? does it shows just transparent picker? Also, do you include UIPickerViewDelegate and UIPickerViewDataSource in .h file? – Yanchi Oct 01 '13 at 17:11
  • Hmm, did you delete pickerView:titleForRow:forComponent: from your code? It has to be either titleForRow or viewForRow. Dont forget to implement your other delegate methods (numberOfComponents and numberOfRowInComponent) – Yanchi Oct 01 '13 at 17:15
  • I am trying to add `UIView` under `UIPickerView` because I need to use standard `UIPickerView` on iOS 5 and 6. – Dmitry Oct 01 '13 at 17:39
  • Hmm, maybe you can find some inspiration here http://stackoverflow.com/questions/7848766/how-can-we-programmatically-detect-which-ios-version-is-device-running-on Just find OS version of your customer and split up your code to support older versions – Yanchi Oct 01 '13 at 17:44
  • But if I use `(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component` I must create view for all iOS versions. – Dmitry Oct 01 '13 at 18:31
9

This worked for me, in iOS 7.1:

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

This changes the color of all pickers. You can put a conditional around it if you only want this to run on devices with iOS 7.

arlomedia
  • 8,534
  • 5
  • 60
  • 108
8

I have added UIView under UIPickerView with code:

CGRect framePickerView = CGRectMake(0, self.view.frame.size.height, self.view.frame.size.width, 216);
pickerView = [[[UIView alloc] initWithFrame:framePickerView] autorelease];
pickerView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:pickerView];
[pickerView addSubview:picker];

instead the code:

[self.view addSubview:picker];
Dmitry
  • 14,306
  • 23
  • 105
  • 189
5

Even though I set UIPickerView.backgorundColor, but I had weird background color.

removing following line fixed the issue:

UITextField.keyboardAppearance = .dark

Or just reset keyboardAppearance back to default, like so:

UITextField.keyboardAppearance = .default
AamirR
  • 11,672
  • 4
  • 59
  • 73
  • I _never_ would have thought setting keyboardAppearance would cause my pickerView background color not to work. Thank you!! – thattyson Oct 18 '18 at 05:31
  • This solved it for me. I had my UITextField subclassed and in that subclass I was setting the keyboard appearance. This worked well for textfields using the normal keyboard. But this indeed messed with textfields using the UIPicker as input, making the background of the UIPicker not setable. Just for info: funny enough, textfields using the UIDatePicker, the backgroundcolor of UIDatePicker was setable. – guido Dec 10 '18 at 15:32
3

For anyone working in swift, and potentially using multiple pickers:

pickerView1.backgroundColor = UIColor.darkGrayColor()
pickerView2.backgroundColor = UIColor.greenColor()

Xcode 7.3

Diesel
  • 519
  • 1
  • 6
  • 24