From the course: iOS Development Tips

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

UIColor class extensions

UIColor class extensions

- [Instructor] Couple of weeks ago there was a tip video on converting hex colors to UIColor. That was a function you had to copy and paste into your code. I've also shown you how to use the assets to keep color names handy. What would be even better is to do all that directly from UIColor. For that, you use extensions. Extensions have many uses, but basically they add functionality to a class, very often a class you can't change, such as UIColor. You can have a separate file with the extension and that can be ported to any new project. Download the starter project. I've set it up with the hexColor function in the view controller to change the background color of the app. You'll see in the view controller, I set view.backgroundColor equal to yellow. But I'd like to use the four colors of a pizza. What I want to use is tomato in a declaration like this, view.backgroundColor dot T-O-M-A-T-O, tomato. Now obviously there is no member tomato, but to do this, I'll make an extension. So go…

Contents