如何在UIPickerView中更改背景颜色?

4

我想改变 UIPickerView 的背景颜色,这可行吗?

4个回答

7

更新:一个简单的技巧是在某些子视图上设置alpha。具体取决于你有多少个组件,但代码如下:

[(UIView*)[[picker subviews] objectAtIndex:2] setAlpha:0.5f];

这将使表盘半透明,并显示背景颜色。


设置backgroundColor似乎没有任何作用。到目前为止,我所能做的最好的事情是设置单个组件行的背景颜色,但这几乎没有用,因为组件的基本颜色仍然是白色。

理想情况下,我想设置每个组件(即表盘)的背景颜色。


3

最后终于做到了。我试着为一个具有一个组件的 picker view 进行操作。

  1. set the picker view background color to clear color by wizard or by code as follow:

    Picker1.backgroundColor = [UIColor clearColor];
    
  2. Set alpha value to 0.0f for the picker view subviews numbered 0, 1 and 3. (Don't know why there is subview 2 thought). Do it by code after the first load data for the picker view as follow (it will throw an exception if you do this in the DidViewLoad).

    [(UIView*)[[Picker1 subviews] objectAtIndex:0] setAlpha:0.0f];
    [(UIView*)[[Picker1 subviews] objectAtIndex:1] setAlpha:0.0f];
    [(UIView*)[[Picker1 subviews] objectAtIndex:3] setAlpha:0.0f];
    
  3. Don't forget to clear background color for the label you are sending to the picker view in the viewForRow method.

    lbl.backgroundColor = [UIColor clearColor];
    

3

似乎iOS 4.2没有任何子视图...

NSLog(@" %@ ", [self.pickerView subviews]); 

返回空数组。


2
它将在pickerview代理中提供子视图-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ - Priyanka V

1
在 iOS 7 上,我只需将自定义背景图像放在其后面即可。不需要改变任何其他东西。

1
iOS7更新加1分。有了你提供的信息,给列设置不同的背景很容易——我只需用Photoshop创建一个合适的条纹背景。在上下边界处加入一些白色渐变。完成!你可以在这里看到它:http://stackoverflow.com/questions/20887001 - DerWOK

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接