UIColor colorWithRed:green:blue:alpha Function Not Working [Solution] – XCode iOS

For example you are trying to add a custom RGB color to background using the following code:

textView.backgroundColor = [UIColor colorWithRed:190 green:179 blue:59 alpha:1];

But it’s not showing the intended color, instead it always shows white in the background. The problem is that the arguments for colorWithRed:green:blue:alpha need to be CGFloats on a scale from 0 to 1, where 0 is 0 and 1 is 255. Therefore, your code should look more like this:

textView.backgroundColor = [UIColor colorWithRed:190.0/255 green:179.0/255 blue:59.0/255 alpha:1.0];
Note: Do not forget the “.0”. Otherwise your operation will be computed with integers. And there the possible results are 0 and 1 only
Namit Gupta

iOS Developer @GetVokal. Passionate about Mobile Apps. Loves to Design. You can reach him on his social profiles below.

Comments 3

Leave a Reply