My Top Swift 4 Extensions You Might Like


Whenever I start building a new iOS app I transfer my favourite extensions from one project to another one. The more I code the more I realise how life becomes easier with these amazing helpers.

In this article I would like to share my favourite Swift extensions I use on daily basis.

Currently I work on migrating UI of the app from a storyboard to code and of course there are some handy dandy extensions I can’t live without.

  • When you want to pin your view to the edges of the superview:
Trailing and leading anchors are used when you handle different orienations. You might replace it with left and right anchors if your device orienation is only portrait.
  • This one is used to set some specific constraints, width or height:
When you call the function on a view you might remove unused parameters as they become nil or 0 by default
  • In case if you need to handle setting constraints with safeAreaLayoutGuide:
NSLayoutAnchor is available since iOS 9.0 +
  • It’s not necessary anymore to create a constant with cellId, all you need is to pass cell’s class name as a cell type with .self in the end:
Ex: tableView.register(CustomCell.self)
  • A color extension is very useful when you want to save some space and make sure you pass a correct color code. You can specify RGB color:
Assertions will be triggered to prevent passing an incorrect rgb value
  • Also you can convert HEX to UIColor in this way:
  • When you programmatically create buttons, labels or textfields some of them you might customize with a convenience initialiser:
With this example in hands you can make the same initialiser for other UIView items

Extensions are very powerful and can be used in many other ways to make your code look cleaner, more readable and easy to understand.

This is my first attemp on Medium to put some knowledge and make someone’s life easier. I would appreciate any support from you guys and thanks for reading!

Enjoy your day and stay positive :)