Swift Helper Extensions Every Beginner iOS Dev Must Use

SwiftDev365
Sep 23, 2019 · 3 min read

When I first started iOS development, I didn’t have anyone who could help point me in the right direction when it came to best practices, tips, and tricks. So I struggled a bit in those early days, especially when I moved from storyboard layouts to building apps programmatically.

Enter extensions, they are your best friend! Have you ever thought there has to be an easier way of doing this? With extensions, there is. extensions are going to supercharge your iOS development productivity.

Here are a few Swift helper extensions that I wish I’d had when I first started iOS development, so I hope they’ll be able to help you in your journey.

1. Adding UI and applying constraints to your views

This is by far my #1 must-have helper extension you must incorporate into your iOS development. This extension allows you to place your UI objects, such as UILabel, UIImage, CollectionView, TableViews, etc. Then apply constraints, all in one awesome little UIView extension.

The code:

Usage:

addSubview(postButton)postButton.apply(top: topAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, marginTop: 0, marginLeft: 0, marginBottom: 0, marginRight: 0, width: 0, height: 0)postButton.centerX(inView: self)
postButton.centerY(inView: self)
postButton.center(inView: self)

an example of applying objects next to each other and spacing them out with the margin properties:

addSubview(userAvatar)
userAvatar.apply(top: topAnchor, left: leftAnchor, bottom: nil, right: nil, marginTop: 10, marginLeft: 16, marginBottom: 0, marginRight: 0, width: 50, height: 50)
addSubview(usernameLabel)
usernameLabel.apply(top: topAnchor, left: userAvatar.rightAnchor, bottom: nil, right: rightAnchor, marginTop: 10, marginLeft: 8, marginBottom: 0, marginRight: 8, width: 0, height: 30)

If you care as much about code tidiness as I do, you do what you can to keep your code clean and minimalist. Because we have given defaults within our extension we can just remove any properties that we aren’t using.

example of code simplified by removing unused properties:

addSubview(userAvatar)
userAvatar.apply(top: topAnchor, left: leftAnchor, marginTop: 10, marginLeft: 16, width: 50, height: 50)
addSubview(usernameLabel)
usernameLabel.apply(top: topAnchor, left: userAvatar.rightAnchor, right: rightAnchor, marginTop: 10, marginLeft: 8, marginRight: 8, height: 30)

2. RGBA UIColor Extension

Being a professional designer for the past 18 years I have never really been too impressed with how iOS deals with color. But luckily we have these next few extensions to help us out.

The code:

Usage:

static let primaryBlue = UIColor.rgba(red: 0, green: 122, blue: 255, alpha: 1)
static let transparentBlack = UIColor.rgba(red: 0, green: 0, blue: 0, alpha: 0.5)

3. HEX > UIColor Extension

Web and app product designers tend to work mostly in #HEX based color definitions, and so you’ll most likely get and color specs in this format. Having an extension that allows you to simply convert that Hex value to something swift can use.

The code:

Usage:

static let midGrey = UIColor(hexColor: “#3f3f3f”)
static let midGreyAlpha = UIColor(hexColor: “#CCCCCC”, alpha: 0.5)

These are my top 3 helper extensions that I use every day on every project.

I’m going to be posting more Beginner based posts, as there are tons of useful information that I feel a lot of beginners might be looking for, so keep an eye out.

SwiftDev365

Written by

18 years working in tech as a Product Designer, Web Developer, and iOS Developer. Here to help those looking to learn and grow.

More From Medium

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