Setting up backgrounds for UIView

There are different ways of setting up backgrounds in Swift, depending on the object, method, background type etc.
We will go through some of these here.

Setting up a background image for a UIView:

Replace “myImage.png” with the name of your image.
Note: Make sure you add your images to the Bundle first.
To do that – right click anywhere in the bundle files in the Project Navigator and
select ‘Add Files to “project_name”‘, you can also drag the images there from Finder:

Make sure you have the ‘Copy the items if needed’ option checked.

To simply set the color of a UIView you would use this:

replace view or view2 with the name of your UIView and the color of your choice.
Once you type UIColor. you should get a list of the available UIColors.
Just in case here are the ones provided by Apple as a UIColor:

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

In case you want to use RGB colors you can use a function like this:

You would use the function like this:

Another way of doing it would be like this: