5+1 Swift extensions to make your life easier

Balázs Vincze
Jan 25 · 3 min read

Thanks for stopping by, today I’ll show you five small Swift extensions I use in almost every iOS project.

🏁 Here we go:

1. Padding a UIButton

I often find myself having to add some padding to a UIButton. Instead of tweaking around with the titleEdgesInsets and contentEdgeInsets, I created this simple CGSize extension, which I then use in UIButton subclasses:

2. Highlighting parts of a String

It’s common to implement some sort of partial string highlighting when adding autocomplete to an app. To save myself some time, I use the following extension. The example demonstrates usage with a UILabel.

3. Subscripting Array using an IndexPath

Have you ever written something like this in your tableView(_:cellForRowAt:) implementation?

Binding data to your cells using the row’s index path as the array subscript is common practice. I almost write this line of code (or something very similar) everyday, and sometimes I still forget to append .row to the index path. To save myself 0.15 seconds each time, I extended the default Swift Array with this simple function:

4. Finding superviews with a given type

Have you ever wanted to access your table view cell from a textfield which is embedded in that cell? Maybe access the collection view from a collection view cell subclass?

I got you covered:

5. Embedding a View Controller inside another View Controller, a.k.a. container views

A major problem with Apple’s MVC approach lies in its name. They are Massive View Controllers! Picking an architecture is actually a very debated topic among iOS developers.

My five cents: It’s completely fine to stick with the MVC pattern, as long as you enforce the single-responsibility principle and use container views inside your view controllers. This makes sure that a view controller only has one job and keeps your implementation rather simple. An added benefit is that you can reuse your view controllers very easily, and complex UIs become quite manageable, as they are built up from compact UI components which all manage their own state.

The extension I use to embed view controllers:

5+1 Managing colors in your app

With the release of iOS 11, Apple introduced named colors. This lets you organize your colors in Asset catalogs, just like you would do with your images, super neat. One problem with this is having to reference UIColors like this in your code:

This is of course quite error-prone (imagine having to reference that color in 10 different places in your app), and it would be way nicer to take advantage of Swift’s type-safety (which also gives us autocomplete).

We can easily achieve this by extending UIColor with computed properties for our named colors:

Tip: Don’t name your colors “Blue”,“LightGray” or etc. Instead name them by their role in your UI: “Accent“, “ Primary”, “SecondaryText”, etc. This will avoid confusion when trying to name a color (is it purple or dark blue)? Also, it will be super easy to change the app’s color scheme down the road, should you need to.

🎉 Thanks for reading!

I hope you found something useful for your own projects.

Are you also a lazy-smart developer? What extensions do you use in your code? Let me know in the comments!

See how to handle fonts in your iOS app in a better way! 🤖

Balázs Vincze

Written by

I build iOS apps!

Flawless iOS

🍏 Community around iOS development, mobile design, and marketing

More From Medium

More from Flawless iOS

More from Flawless iOS

More from Flawless iOS

NSTimer vs CADisplayLink

More from Flawless iOS

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade