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

Technical Lead iOS @ONEChampionship | Ex-SDE2 @ Halodoc, Ex-iOS Lead @ Vokal. ❤️ Swift, designing pixel-perfect UI and writing clean code. Check my portfolio here.

Comments 4

Leave a Reply