Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set the background color of UIPickerView on iOS 7 using SDK 7?

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.

like image 206
Dmitry Avatar asked Oct 01 '13 16:10

Dmitry


4 Answers

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

like image 102
Jesse Avatar answered Nov 01 '22 03:11

Jesse


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

like image 30
Yanchi Avatar answered Nov 01 '22 02:11

Yanchi


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.

like image 9
arlomedia Avatar answered Nov 01 '22 02:11

arlomedia


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];
like image 8
Dmitry Avatar answered Nov 01 '22 04:11

Dmitry