How to change the background colour of a UIView

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.

Leave a Reply