【问题标题】:How to change the Background color of UIPickerView in iPhone如何在 iPhone 中更改 UIPickerView 的背景颜色
【发布时间】:2011-02-19 09:22:51
【问题描述】:

我在我的应用程序中使用了选择器视图控制器。现在我想改变背景颜色的颜色。那么我如何更改选择器视图的背景颜色并且还想更改行背景颜色。我想更改选择器视图的默认布局颜色,是否可以将自定义图像设置为选择器视图的背景?所以请指导我。

【问题讨论】:

  • 你不能改变 UIPickerView 的颜色....

标签: iphone uipickerview


【解决方案1】:

我已经answeredsimilar question。您可以通过使用 IXPickerOverlayView 类 (available on GitHub) 完全替换背景图像来更改背景颜色。

【讨论】:

    【解决方案2】:

    您可以将UIView 添加到UIPickerView 的组件中,但您不能更改背景颜色或色调颜色。

    • viewForRow:forComponent 用于查看。

    【讨论】:

      【解决方案3】:

      终于成功了。我尝试使用一个组件的选择器视图。

      通过向导或代码设置选择器视图背景颜色如下:

      Picker1.backgroundColor = [UIColor someColor];// clearColor for transparent
      

      为选择器视图子视图设置 alpha 值,编号为 0、1 和 3。(不知道为什么会有子视图 2 的想法)。在第一次为选取器视图加载数据后通过代码执行此操作,如下所示(如果您在 DidViewLoad 中执行此操作,则会引发异常)。

      [(UIView*)[[Picker1 subviews] objectAtIndex:0] setAlpha:someValue];// 0.0f for transparent
      [(UIView*)[[Picker1 subviews] objectAtIndex:1] setAlpha:someValue];
      [(UIView*)[[Picker1 subviews] objectAtIndex:3] setAlpha:someValue];
      

      不要忘记在 viewForRow 方法中清除要发送到选取器视图的标签的背景颜色。

      lbl.backgroundColor = [UIColor someColor];// could be clearColor
      

      【讨论】:

        【解决方案4】:

        我最近写了an article about just this topic。很容易实现你想做的事情。 (虽然本文重点关注 iOS7,但无论现代 iOS 版本如何,为组件行提供视图而不是字符串的技术都有效。)

        【讨论】:

          【解决方案5】:

          我已更改 UIPicker 背景颜色和 UIPicker 工具栏颜色和字体颜色。让您的 UIPickerView 名称为 pickerView。

          pickerView.backgroundColor=[UIColor greenColor]; - 在 greenColor 的地方你可以使用任何颜色

          pickerView.backgroundColor=[UIColor colorWithRed: 127 green:127 blue:127 alpha:1]; - 在这里您可以选择任何颜色代码。

          假设您在 UIPickerView 中使用了工具栏,那么我们可以使用 toolBar.backgroundcolor=[UIColor greenColor]; - 在 greenColor 的地方你可以使用任何颜色

          toolBar.backgroundColor=[UIColor colorWithRed: 127 green:127 blue:127 alpha:1]; - 在这里您可以选择任何颜色代码。

          toolBar.backgroundImage=[UIImage imageWithName:@"imageName"]; - 您可以在工具栏中将图像设置为背景图像。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2021-12-27
            • 1970-01-01
            • 1970-01-01
            • 2011-03-26
            • 1970-01-01
            • 2011-11-11
            • 1970-01-01
            • 2010-11-01
            相关资源
            最近更新 更多