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

僤鯓⒐⒋嵵緔 提交于 2019-12-03 23:03:47

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];

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

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

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

Xcode 7.3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!