ios 具有自定义背景颜色的自定义 UIPickerView

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11426219/
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-30 19:13:31  来源:igfitidea点击:

Custom UIPickerView with Custom Background color

iosobjective-ccocoa-touchuipickerviewpicker

提问by Perseus

I have created a sample picker-view which will display some details. Setting the Background color using:

我创建了一个示例选择器视图,它将显示一些详细信息。使用以下方法设置背景颜色:

_pickerView.backgroundColor = [UIColor redColor]; 

doesn't seems to be working . But I wish I could create a picker view like the below image

似乎不起作用。但我希望我可以创建一个如下图所示的选择器视图

enter image description here

在此处输入图片说明

What I was trying to do is , the entire PickerView background color should not be white, it should fill with the UIColorwhich I am giving. Is that possible?? How it can be done ??

我试图做的是,整个 PickerView 背景颜色不应该是白色,它应该填充UIColor我提供的颜色。那可能吗??怎么办??

回答by DShah

Addition to Nina's answer, below are some of the good custom Picker view controls which will be good to use in terms of performance and customizable.

除了 Nina 的回答之外,下面是一些很好的自定义 Picker 视图控件,它们在性能和可定制性方面非常有用。

  1. http://www.cocoacontrols.com/platforms/ios/controls/cppickerview
  2. http://www.cocoacontrols.com/platforms/ios/controls/afpickerview
  3. http://www.cocoacontrols.com/platforms/ios/controls/v8horizontalpickerview(Horizontal PickerView)
  1. http://www.cocoacontrols.com/platforms/ios/controls/cppickerview
  2. http://www.cocoacontrols.com/platforms/ios/controls/afpickerview
  3. http://www.cocoacontrols.com/platforms/ios/controls/v8horizo​​ntalpickerview(Horizo​​ntal PickerView)

回答by Derek Ju

I wrote a custom picker view that uses a UITableView as it's starting point. Feel free to customize it however you want.

我编写了一个自定义选择器视图,它使用 UITableView 作为起点。随意自定义它。

https://github.com/derekju/DjuPickerView

https://github.com/derekju/DjuPickerView

回答by Filipe Alvarenga

It is hard to customize a UIPickerView.

很难自定义 UIPickerView。

You might opt to use it in its default appearance or trying to simulate a UIPickerView using another UIKit component such as UITableView. There are some libs that are using UITableView to simulate a more flexible UIPickerView.

你可以选择使用它的默认外观,或者尝试使用另一个 UIKit 组件(例如 UITableView)来模拟 UIPickerView。有一些库使用 UITableView 来模拟更灵活的 UIPickerView。

回答by PJR

You can change selected color by putting tableview cell in it.

您可以通过将 tableview 单元格放入其中来更改所选颜色。

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UITableViewCell *cell = (UITableViewCell *)view;

if( cell == nil ) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil] autorelease];
    [cell setBackgroundColor:[UIColor redColor]];

    [cell setBounds: CGRectMake(0, 0, cell.frame.size.width -20 , 44)];
    cell.textLabel.text = [self.userNameArray objectAtIndex:row];
    cell.userInteractionEnabled = NO;
}

return cell;
}

回答by Nina

UIPickerViewhas subviews. Assigning an image to a view and adding it to the subview at index 2, would change the background of pickerview.

UIPickerView有子视图。将图像分配给视图并将其添加到索引 2 处的子视图,将更改 pickerview 的背景。

I had had a same kinda problem :-) See my question here!! Customize UIPickerView's Skin with images

我遇到了同样的问题:-) 在这里查看我的问题!!使用图像自定义 UIPickerView 的皮肤

Or you can use AFPickerView

或者你可以使用AFPickerView

回答by Kuldeep

The API does not provide a way to change the style and color of selection dialer. You can change only inner view of the picker how u want to design. As the UIPickerView don't have the UI_APPEARANCE_SELECTOR.

API 不提供更改选择拨号器的样式和颜色的方法。您只能更改选择器的内部视图如何设计。由于 UIPickerView 没有 UI_APPEARANCE_SELECTOR。

Check out below link will help you out. Custom iOS UIDatepicker using UIAppearance

看看下面的链接会帮助你。 使用 UIAppearance 自定义 iOS UIDatepicker