How to change the background colour of a UIView

You can use the backgroundColor property for this:

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:

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

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