【问题标题】:UIButton with UITableViewCellSelectionStyleGray Selection Color带有 UITableViewCellSelectionStyleGray 选择颜色的 UIButton
【发布时间】:2013-09-20 20:22:26
【问题描述】:

我正在尝试将 UIButton 的选定颜色设置为“UITableViewCellSelectionStyleGray”的颜色;问题是无法设置 UIButton 的选择颜色,只能设置图像。有谁知道如何使用 UITableViewCellSelectionStyleGray 制作与所选单元格颜色完全相同的图像?

【问题讨论】:

  • 您是在代码中还是在 NIB/Storyboard 中创建 UIButton
  • 我正在代码中创建一个 UIButton。

标签: ios objective-c uitableview uibutton selection


【解决方案1】:

不幸的是,我似乎无法以编程方式找到实际的颜色,但 DigitalColor Meter (OSX APP) 说颜色是 217 217 217

UIColor* selectedColor = [UIColor colorWithRed:217.0/255.0 
                                         green:217.0/255.0 
                                          blue:217.0/255.0 alpha:1.0];

【讨论】:

  • 不错,就是UITableViewCellSelectionStyleGray的颜色!
  • ...或者只是 UIColor(white: 217.0 / 255.0, alpha: 1.0)
【解决方案2】:

当您在UIButton 上设置tintColor 属性时,颜色将设置为突出显示状态。所以你可以这样做:

[button setTintColor:[UIColor lightGrayColor]];

但是,lightGrayColor 可能不是您正在寻找的确切灰色。我不知道有一种方法可以参考UITableViewCell 的确切选择灰色。您可能只需要使用[UIColor colorWithRed:green:blue:alpha:] 就可以得到您想要的。

**同样,tintColor 属性在 iOS 7 中也没有不同的含义,所以如果您使用此代码,请注意这一点。

【讨论】:

  • iOS7 确实需要这个。而且我实际上正在寻找 iOS 7 中的默认选择灰色。我已经尝试过这个,但我认为它在任何情况下都不适用于 iOS 7。感谢您的帮助。
【解决方案3】:

试试这个:

UIButton    *button =   [UIButton buttonWithType:UIButtonTypeCustom];
button.frame    =   CGRectMake(50, 50, 60, 20);
button.backgroundColor  =   [UIColor colorWithPatternImage:[UIImage imageNamed:@"orange.jpg"]];
[self.view addSubview:button];

【讨论】:

  • 我认为你没有理解我的问题。
  • 你可以使用配色器来选择你想要的颜色并放入
  • 我仍然认为您没有阅读过这个问题。无法设置选择颜色。
  • 您希望按钮颜色在选中时为灰色??
  • 不,按钮不在单元格上。很抱歉,我无法继续此对话。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多