如何实现UIPickerView如图所示,具有组件标题?

时间:2013-02-07 06:58:43

标签: iphone ios uipickerview

enter image description here

如何实现这样的选择器视图?我已经实现了所有必要的delegate和dataSource方法来填充数据,但我无法理解的是如何添加这些标题成人儿童婴儿? 它们是静态的,不会随组件旋转!

5 个答案:

答案 0 :(得分:1)

您可以将标签放在特定的框架位置,然后将标签背景颜色设置为clearColor。

答案 1 :(得分:1)

当您显示选择器视图时,将3个标签作为子视图添加到视图中,然后在选择器被关闭时隐藏它们。

您必须将标签放在乐队上。

答案 2 :(得分:0)

您需要添加标签作为选择器视图的子视图。 UIPickerView没有内置功能来简化这一过程。

答案 3 :(得分:0)

创建选择器,创建带阴影的标签,并将其推送到selectionIndicator视图下方的选择器子视图。

它看起来像这样

UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(135, 93, 80, 30)] autorelease];
label.text = @"Label";
label.font = [UIFont boldSystemFontOfSize:20];
label.backgroundColor = [UIColor clearColor];
label.shadowColor = [UIColor whiteColor];
label.shadowOffset = CGSizeMake (0,1);
[picker insertSubview:label aboveSubview:[picker.subviews objectAtIndex:5]]; 
//When you have multiple components (sections)...
//you will need to find which subview you need to actually get under
//so experiment with that 'objectAtIndex:5'
//
//you can do something like the following to find the view to get on top of
// define @class UIPickerTable;
// NSMutableArray *tables = [[NSMutableArray alloc] init];
// for (id i in picker.subviews) if([i isKindOfClass:[UIPickerTable class]]) [tables addObject:i];
// etc...

答案 4 :(得分:0)

我使用Interface Builder完成了这项工作。

我创建了一个容器视图,然后将选择器视图放入其中。 为了确保我的容器大小与选择器视图相同,我设置了空间限制:前导,尾随,顶部和底部。

然后我在拾取器视图上面放了3个标签(但它们仍然是容器的子视图)并将它们的帧设置为居中。

同样要获得与屏幕截图相同的标签视觉效果(似乎在选择栏下),将标签的alpha降低到约0.7。