Subclassing UIView in Swift

Provide the functionality that a standard UIView just can’t

Steven Curtis
7 min readMay 28, 2020

Here are Apple’s Subclassing notes from their documentation:

The UIView class is a key subclassing point for visual content that also requires user interactions. Although there are many good reasons to subclass UIView, it is recommended that you do so only when the basic UIView class or the standard system views do not provide the capabilities that you need. Subclassing requires more work on your part to implement the view and to tune its performance.

Which is where many stackoverflow posts will point you to. This doesn’t necessarily help that many users, in the way that this guide will with a practical guide as to actually subclass a UIView with examples.

This guide to the rescue!

Photo by Kym Ellis on Unsplash

Difficulty: Beginner | Easy | Normal | Challenging

Prerequisites:

  • Some knowledge of OO would be useful (although I’ll take you through the basics here), and I will use Playgrounds (and include this in full at the end) but if you want to see the result of your work press one of the following symbols (the eye or the rectangle) to see it!

Terminology

--

--