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.
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...
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
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.
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];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With