Custom Colors

Now you have a play button, but the background is a boring white color. You can improve this by assigning a UIColor object to self.backgroundColor. Notice that you construct UIColor by using a hexadecimal value, also called a hexcode. By default, UIColor accepts color values in red-green-blue (RGB) form. To make the hexcode feature work, you add custom functionality to UIColor. Extending a class is fairly simple. Refer to your list of project files and right-click the Create New File button. Choose Swift as the file type and name the file UIColorExtensions.swift. Then add to it the following code:

import UIKitextension UIColor {    convenience init(hex: Int, alpha: CGFloat = 1.0) {        let red = ...

Get Learning Swift™ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.