如何使用 SDK 7 在 iOS 7 上设置 UIPickerView 的背景颜色?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19121799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:55:46  来源:igfitidea点击:

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

iphoneiosios7uipickerview

提问by Dmitry

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

如何UIPickerView在 iOS 7 上使用 SDK 7设置背景颜色并在 iOS 5 和 6 上使用标准选择器?默认情况下,它在 iOS 7 上是透明的。

采纳答案by Dmitry

I have added UIViewunder UIPickerViewwith code:

我加入UIViewUIPickerView的代码:

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

回答by Jesse

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 backgroundColoris a valid property...

我假设你在调用它时有一个对选择器视图的引用,它是 UIView 的子类,所以backgroundColor是一个有效的属性......

回答by Yanchi

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

我想把它写成评论,但很难阅读:) 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

此外,它不必只是标签,我认为您也可以在那里插入其他子视图......据我所知,它适用于 iOS7 图片在这里

回答by arlomedia

This worked for me, in iOS 7.1:

这对我有用,在 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.

这会更改所有选择器的颜色。如果您只希望它在装有 iOS 7 的设备上运行,您可以在它周围放置一个条件。

回答by AamirR

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

即使我设置了 UIPickerView.backgorundColor,但我的背景颜色很奇怪。

removingfollowing line fixed the issue:

删除以下行修复了问题:

UITextField.keyboardAppearance = .dark

Or just reset keyboardAppearanceback to default, like so:

或者只是重置keyboardAppearance回默认值,如下所示:

UITextField.keyboardAppearance = .default

回答by Diesel

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

对于任何在 swift 中工作并可能使用多个选择器的人:

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

Xcode 7.3

Xcode 7.3