在 IOS 中使用 RGB 颜色的问题

Issue with using RGB colors in IOS

我想使用 RGB 颜色来设置 UIButton 的背景颜色。

我有很多 UIButtons 所以我决定自定义 class.But 当我尝试使用 colorWithRed 我收到错误。

First Code:

[self.layer setBackgroundColor:[UIColor colorWithRed:60/255.0f green:146/255.0f blue:180/255.0f alpha:1.0f]];

错误

Second Code

  self.layer.backgroundColor = [UIColor colorWithRed:60/255.0f green:146/255.0f blue:180/255.0f alpha:1.0f];

错误

尝试使用此代码

self.layer.backgroundColor = [UIColor colorWithRed:60/255.0f green:146/255.0f blue:180/255.0f alpha:1.0f].CGColor;

您需要将 UIColor 转换为 CGColor,这可以通过上面的代码完成。

希望这能解决您的问题

layer backgroundcolor是cgcolorref类型的,不能用uicolor。我们需要使用cgcolor或者将uicolor转换成cgcolor再使用。

示例:

[[UIColor whiteColor] CGColor];

[UIColor whiteColor].CGColor;