问题
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.
回答1:
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...
回答2:
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
回答3:
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.
回答4:
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];
回答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
回答6:
For anyone working in swift, and potentially using multiple pickers:
pickerView1.backgroundColor = UIColor.darkGrayColor()
pickerView2.backgroundColor = UIColor.greenColor()
Xcode 7.3
来源:https://stackoverflow.com/questions/19121799/how-to-set-the-background-color-of-uipickerview-on-ios-7-using-sdk-7