How to change the background colour of a UIView

- by

You can use the backgroundColor property for this:

self.yourView.backgroundColor = [UIColor purpleColor];

Pre-defined values are

  • blackColor
  • darkGrayColor
  • lightGrayColor
  • whiteColor
  • grayColor
  • redColor
  • greenColor
  • blueColor
  • cyanColor
  • yellowColor
  • magentaColor
  • orangeColor
  • purpleColor
  • brownColor
  • clearColor

You can also define your own colours by creating a UIColor object (from RGB or HSL values). Here’s how you can create your own RGB colour and use it with the above example:

UIColor *myPurple = [[UIColor alloc]initWithRed:1 green:0.5 blue:1 alpha:1];
self.yourView.backgroundColor = myPurple;

RGB and alpha must be defined as CGFloat numbers, i.e. between 0 and 1. The alpha value is the opacity, so 1 is fully visible and 0 is invisible.



If you enjoy my content, please consider supporting me on Ko-fi. In return you can browse this whole site without any pesky ads! More details here.

Leave a Comment!

This site uses Akismet to reduce spam. Learn how your comment data is processed.